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

时间:2015-03-19 21:10:41

标签: java android

我知道如何让它无法点击“button.setEnabled(false);”但不知道如何调用该函数。我试过if(editText.setText())。任何帮助都表示赞赏,并提前感谢。

5 个答案:

答案 0 :(得分:3)

editText.setText()是一个void方法,你想得到一个返回东西的方法(gettext)。

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

答案 1 :(得分:0)

您可以使用addTextChangedListener方法向TextWatcher添加EditText,并在onTextChanged或{{3}处实施启用\停用按钮的所有必要逻辑方法。

答案 2 :(得分:0)

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

只需一行解决方案。

答案 3 :(得分:0)

我认为正确的方法是向edittext添加一个点击监听器,然后从那里监控onTextChanged并在字符串长度为0时禁用该按钮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);
        }
    });

答案 4 :(得分:0)

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