Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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
Java Android EditText字符串等于预定义字符串_Java_Android - Fatal编程技术网

Java Android EditText字符串等于预定义字符串

Java Android EditText字符串等于预定义字符串,java,android,Java,Android,我只是Android编程的初学者,我有一个问题,我一直在试图找到一种使用if语句来启用按钮的方法。我想要的是,如果用户输入到EditText中的内容等于我定义的字符串,我希望启用按钮,否则禁用按钮。我用过TextWatcher,我可以用整数来完成这个任务,但我想知道字符串是否也可以?非常感谢您提供的任何帮助如果用户没有在edittext中输入任何数据,您可以在edittext上使用setError。单击按钮后,使用if语句检查edittext中的文本是否等于您定义的字符串。如果没有,请使用set

我只是Android编程的初学者,我有一个问题,我一直在试图找到一种使用if语句来启用按钮的方法。我想要的是,如果用户输入到EditText中的内容等于我定义的字符串,我希望启用按钮,否则禁用按钮。我用过TextWatcher,我可以用整数来完成这个任务,但我想知道字符串是否也可以?非常感谢您提供的任何帮助

如果用户没有在
edittext
中输入任何数据,您可以在edittext上使用
setError
。单击按钮后,使用
if
语句检查
edittext
中的文本是否等于您定义的字符串。如果没有,请使用
setError
edittext
显示错误消息并返回
false
。因此,只有当函数返回true时,才允许启用按钮的功能

否则您可以尝试,
myButton.setEnabled(false)
当字符串匹配时,将其设置为true

如果您想要XML等价物,请尝试
android:clickable

String s= et.getText().toString();
    if(s.equals("hurray"))
    {
        b.setClickable(true);

    }

是的,您可以使用textchange listener收听文本

yourEditText.addTextChangedListener(new TextWatcher() {

      public void afterTextChanged(Editable s) {

        // you can call or do what you want with your EditText here
        // compare the editable text s with your fixed string. 
        // if this matched with your predefined text. enable your button click. 

      }

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

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

非常感谢,伙计,谢谢你。