Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 禁用按钮_Android_Kotlin - Fatal编程技术网

Android 禁用按钮

Android 禁用按钮,android,kotlin,Android,Kotlin,你好,我想这样做:我有两个EditText和一个按钮,如果其中一个EditText为空,我想禁用该按钮。为此,我尝试以下方法: if(editText1!!.getText().length == 0 || editText2!!.getText().length == 0 ){ button1!!.setEnabled(false) } 问题是我的按钮每次都被禁用。。。当两个编辑文本不为空时,我可以点击按钮 我该怎么做 谢谢大家! 编辑:在OP澄清

你好,我想这样做:我有两个EditText和一个按钮,如果其中一个EditText为空,我想禁用该按钮。为此,我尝试以下方法:

if(editText1!!.getText().length  == 0 || editText2!!.getText().length  == 0 ){
            button1!!.setEnabled(false)
        }
问题是我的按钮每次都被禁用。。。当两个编辑文本不为空时,我可以点击按钮

我该怎么做


谢谢大家!

编辑:在OP澄清后。 如果你想检查两个编辑文本,你可以做如下操作

        boolean editText1Empty = true;
        boolean editText2Empty = true;

        editText1.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) {
                editText1Empty = charSequence.length() == 0;
                checkButton();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        editText2.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) {
                editText2Empty = charSequence.length() == 0;
                checkButton();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        void checkButton() {
        button.setEnable(!editText1Empty && !editText2Empty);
        }

使用textwatcher——谢谢你,我确实认为我必须使用textwatcher,但问题是我有两个editText…现在检查。我已经更新了答案。您可以将条件作为参数写入
setEnabled
即cleaver:)但我认为这将使OP更清晰。我将仅将其设置为一个
TextWatcher
,并检查
EditText.isEmpty()
上的
onTextChanged()
。我想它会更纯净。