Chunhua
Chunhua

Reputation: 51

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?

Upvotes: 4

Views: 6216

Answers (2)

Thiago
Thiago

Reputation: 13302

In Kotlin like so:

android:imeOptions="actionDone"

edittext.onFocusChangeListener = OnFocusChangeListener { view, hasFocus ->
                if (hasFocus) {
                    //Logic Here
                }
            }

Upvotes: 0

Flutterian
Flutterian

Reputation: 1781

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("");
        }
    }
});

Upvotes: 12

Related Questions