Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listener for Done button on EditText and clear EditText when user not press Done button?

I have an EditText and I want to listen for if the user presses the "done" button on the keypad and I also want to clear EditText when user not presses the "done" button on the softkeypad , how would I do this?

like image 773
Chunhua Avatar asked Oct 16 '25 23:10

Chunhua


1 Answers

To check user pressed "Done" button of soft keyboard, use the code below:

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if(i== EditorInfo.IME_ACTION_DONE){
            Toast.makeText(getApplicationContext(),"Done pressed",Toast.LENGTH_SHORT).show();
        }
        return false;
    }
});

To clear the text of edittext once focus has been changed, use the code below:

edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(!hasFocus){
            edittext.setText("");
        }
    }
});
like image 162
Flutterian Avatar answered Oct 18 '25 15:10

Flutterian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!