Detection when the user enters data in edittext, immediately shows the response

I am new to Android development. I want to know when we enter data in editext like (calculation field), it should immediately show the result, without using alertdialog or the button to get the data. Can anyone help me.

+5
source share
3 answers

Since you have a rather abstract question, let me try an equally general answer:

In onCreate()declare and addEditText

EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(filterTextWatcher);

And then out onCreate() , configure filterTextWatcheras follows:

private TextWatcher filterTextWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // DO THE CALCULATIONS HERE AND SHOW THE RESULT AS PER YOUR CALCULATIONS

        int radius = 0;
        radius = Integer.valueof(s.toString);
        double area = Math.PI * radius * radius;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

EDIT:

Updated code with possible calculation. (INCORRECT CODE: I ONLY SEE THIS WITHOUT TESTING THIS. CHANGE WHERE IT IS NECESSARY)

TextWatcher

, :

+14

TextWatcher EditText , . .

,

final EditText EditTxtFinancialCode = (EditText) findViewById(R.id.edtNewCpFinancialCode);


    EditTxtFinancialCode.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            String a = "";
            boolean flag = true;
            String eachBlock[] = EditTxtFinancialCode.getText().toString().split("-");
            for (int i = 0; i < eachBlock.length; i++) {
                if (eachBlock[i].length() > 4) {
                    flag = false;
                }
            }
            if (flag) {

                EditTxtFinancialCode.setOnKeyListener(new OnKeyListener() {

                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {

                        if (keyCode == KeyEvent.KEYCODE_DEL)
                            KeyDel = 1;
                        return false;
                    }

                });

                if (KeyDel == 0) {

                    if (((EditTxtFinancialCode.getText().length() + 1) % 5) == 0) {

                        if (EditTxtFinancialCode.getText().toString().split("-").length <= 2) {
                            EditTxtFinancialCode.setText(EditTxtFinancialCode.getText() + "-");
                            EditTxtFinancialCode.setSelection(EditTxtFinancialCode.getText().length());
                        }

                    }
                    a = EditTxtFinancialCode.getText().toString();
                } else {
                    a = EditTxtFinancialCode.getText().toString();
                    KeyDel = 0;
                }

            } else {
                EditTxtFinancialCode.setText(a);

            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });
+5

http://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html

.

   myTextBox.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

   public void beforeTextChanged(CharSequence s, int start, 
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start, 
     int before, int count) {
   TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
   myOutputBox.setText(s);
   }
  });

. http://www.mysamplecode.com/2012/06/android-edittext-text-change-listener.html

+2

All Articles