Enter the data in the EditText and respond without pressing Enter.

I have an editText with a listener.

edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {

            // Perform action on key press
            return true;
        }
        return false;
    }
});

Now I want to use the data entered in the text field by the user in my program, without clicking on the user. (If I delete (keyCode == KeyEvent.KEYCODE_ENTER), the program freezes for some reason).

What should I do to determine that a user has entered a character in a text box?

+3
source share
1 answer

I think that you want to implement functions such as whenever the user enters any character that you want to check, if so, that is TextWatcher for you. In which you can override its 3 methods: afterTextChanged, beforeTextChangedand onTextChanged.

mPasswordLength = (EditText)findViewById(R.id.password_length);
mPasswordLength.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) {
    }
});
+8
source

All Articles