如果编辑文本字段为空,则禁用按钮。无法正常工作

时间:2018-11-12 11:00:38

标签: android

目前,我正在学习编码,遇到一个问题。我需要禁用上载按钮,直到“编辑文本”字段是某种符号而不是默认提示 Full code

    private TextWatcher EmptyEdit = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.toString().trim().length()==0)
        {
            mUpload.setEnabled(false);
        } else {
            mUpload.setEnabled(true);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

3 个答案:

答案 0 :(得分:2)

将您的代码放在afterTextChanged()内。

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
    mUpload.setEnabled(!TextUtils.isEmpty(s.toString().trim()))
}

// don't forget to set listner
editText.addTextChangedListener(EmptyEdit);

答案 1 :(得分:2)

在afterTextChanged()中而不是onTextChanged()中添加代码。

 private TextWatcher EmptyEdit = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length()==0)
                {
                    mUpload.setEnabled(false);
                } else {
                    mUpload.setEnabled(true);
                }
            }
        };

答案 2 :(得分:1)

    yourEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

     //Disable your button in here

    }
});