开发者

Enter data in EditText and respond without pressing enter

开发者 https://www.devze.com 2023-03-10 06:22 出处:网络
I have an editText with a listener. edittext.set开发者_如何转开发OnKeyListener(new OnKeyListener() {

I have an editText with a listener.

edittext.set开发者_如何转开发OnKeyListener(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 textBox by the user in my program without the user pressing enter. (If I remove (keyCode == KeyEvent.KEYCODE_ENTER) then the program hangs for some reason).

What should I do to identify that the user has entered some character in the textbox?


I think you want to implement functionality like whenever user enter any character you want to check , if this is the case then there is a TextWatcher class for you. In which you can override its 3 methods: afterTextChanged, beforeTextChanged and 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) {
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消