简体   繁体   English

当“编辑文本”框中没有文本时,如何使按钮不可单击?

[英]How can I make a button unclickable when there is no text in the Edit Text box?

I know do know how to make it unclickable "button.setEnabled(false);" 我知道确实知道如何使它不可点击“ button.setEnabled(false);” but not sure how to call the function. 但不确定如何调用该函数。 I have tried if(editText.setText()). 我已经尝试过if(editText.setText())。 Any help is appreciated and thanks in advance. 任何帮助表示赞赏,并在此先感谢。

editText.setText() is a void method, you want to get a method that returns something (gettext). editText.setText()是一个无效方法,您想获得一个返回某些内容(gettext)的方法。

String text = editText.getText().toString();
if (text.equals("")) {
    // do your stuff
}

您可以使用addTextChangedListener方法将TextWatcher添加到EditText并在onTextChangedafterTextChanged方法中实现所有必要的逻辑以启用/禁用按钮。

  yourButton.setEnabled(!yourTextView.getText().toString.isEmpty());

只需一行解决方案。

I think the correct way to do this is to add a on click listener to the edittext , and from there you monitor the onTextChanged and disable the button if the string's length is 0. Which should look something like this: 我认为执行此操作的正确方法是将一个on click侦听器添加到edittext ,然后从那里监视onTextChanged并在字符串的长度为0时禁用该按钮。看起来应该像这样:

    myButton = (Button) findViewById(R.id.button);
    myButton.setEnabled(false);
    EditText textMessage = (EditText)findViewById(R.id.editText);
    textMessage.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {}

        @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.length() == 0)
                myButton.setEnabled(false);
            else
                myButton.setEnabled(true);
        }
    });
you can achive this in two way 

before using any aproach you write the code to disable the button in the 

oncreate() you set the button clickable false by default.
 //1.by using focus change listener
 //2.by using  TextchangeListener or TextWatcher the threshold to check the condition after the specific length of text

public class MainActivity extends Activity {
     private EditText editText;Button button;
    @Override
        public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText= (EditText) findViewById(R.id.editText1);


button= (Button) findViewById(R.id.button1);
            button.setEnabled(false);


    //1.by using focus change listener
     editText.setOnFocusChangeListener(new OnFocusChangeListener(){
    @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(editText.getText().toString().trim().length()>0)
            {    button.setEnabled(true); }
            else{  button.setEnabled(false);}
        }});

    //or
    //2.by using  TextchangeListener or TextWatcher
     editText.addTextChangedListener(new new TextWatcher(){
     @Override
    public void afterTextChanged(Editable s) {

           if(editText.getText().toString().trim().length()>0)
            {   
              button.setEnabled(true);
             } else{  button.setEnabled(false);}



 /*this function will call on every text entry so use
  some threshold so it  will be call once and validate 
    that threshold in if() as condition to make button clickable. */   
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { } });

    }
}

my suggestion is if you want to keep track of the letter than go for the addTextChangedListener 
else use the focusChangeListener code only

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM