Favolas
Favolas

Reputation: 7243

EditText remove focus if setText()

I have an EditText that as an textChangedListener

When I use myEditText.setText("") two things happen:

  1. The keyboard pops up
  2. The texChangedListener (TextWatcher) is fired.

I want to know if there is a way of removing any related events when we use setText()

The first problem I've solved with

if (getWindow() != null) {
       getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
} 

just before myEditText.setText("") but I still think that should be a way of disabling the events from firing in specific situations.

Any idea for the 2 point or how I should handle this events?

Upvotes: 3

Views: 1512

Answers (2)

AChez9
AChez9

Reputation: 158

myEditText.removeTextChangedListener(textWatcher);
myEditText.setText("");
myEditText.addTextChangedListener(textWatcher);

Upvotes: 0

bgauryy
bgauryy

Reputation: 416

set the editText change listener to null, then set its text to empty string, and finally set the change listener again.

Upvotes: 3

Related Questions