Disabling the send button when the EditText is empty when using imeOption= actionSend

前提是你 提交于 2019-11-30 22:05:34
FunkTheMonk

Add a TextChangedListener which will get called whenever the text inside the EditText gets changed.

message.addTextChangedListener(new TextWatcher() {

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    public void afterTextChanged(Editable s) {
        if (s == null || s.length() == 0) {
           send.setEnabled(false);
           message.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
        }
        else {
          send.setEnabled(true);
          message.setImeOptions( /* whatever you previously had */ );
        }
}

Alternatively, you can also let your class implement the TextWatcher interface which makes the code a bit cleaner.

public class MyDialogFragment implements TextWatcher { ... }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!