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

烂漫一生 提交于 2019-11-30 18:04:01

问题


When the text is empty, we want to disable the button (which is how it's set on portrait mode) Any ideas?

Edit: I don't think it's clear but I can enable/disable my own button.. But when using the landscape mode, when the keyboard pops up, the screen is covered by an android specific text area with it's own button (hence the imeOption) So I don't have a problem enabling/disabling the button I have.. It's this Android button that I want to disable when the text area is empty..


回答1:


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 { ... }


来源:https://stackoverflow.com/questions/8259377/disabling-the-send-button-when-the-edittext-is-empty-when-using-imeoption-actio

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