Currently, I need to capture the final appeared text in EditText
.
Here's my simple code.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.edit_text);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
Log.i("CHEOK", ">>>" + editable.toString() + "<<<");
doSomething(editable.toString());
}
private void doSomething() {}
});
}
}
activity_main.xml
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine|textNoSuggestions"
android:imeOptions="actionSend" />
Here's how it behave.
When there's no spelling suggestion
When I press ENTER
afterTextChanged
is being triggered one time with the following output.
>>>123
456<<<
When spelling suggestion is there
When I press ENTER
afterTextChanged
is being triggered multiple time with the following output.
>>>def<<<
>>><<<
>>>abc
<<<
>>>abc
def<<<
I'm not interested the intermediate text in between. I'm only interested in final text (which is abc\ndef
)
How possible can I have something like
@Override
public void afterTextChanged(Editable editable) {
if (isFinalText(edittable.toString())) {
doSomething(editable.toString());
}
}
without turning off spelling suggestion.
P/S
I don't mind to extends EditText
, and do some hacking within it. However, I had tried to override dispatchKeyEvent
and onKeyPreIme
in EditText
. Not sure why they are not being called.
P/S/S
I'm also try onEditorAction
technique as discussed in https://stackoverflow.com/a/8063533/72437 It won't work for my case as my EditText
is multi-line.