Reputation: 51
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
Reputation: 13302
In Kotlin like so:
android:imeOptions="actionDone"
edittext.onFocusChangeListener = OnFocusChangeListener { view, hasFocus ->
if (hasFocus) {
//Logic Here
}
}
Upvotes: 0
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