簡體   English   中英

當“編輯文本”框中沒有文本時,如何使按鈕不可單擊?

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

我知道確實知道如何使它不可點擊“ button.setEnabled(false);” 但不確定如何調用該函數。 我已經嘗試過if(editText.setText())。 任何幫助表示贊賞,並在此先感謝。

editText.setText()是一個無效方法,您想獲得一個返回某些內容(gettext)的方法。

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

您可以使用addTextChangedListener方法將TextWatcher添加到EditText並在onTextChangedafterTextChanged方法中實現所有必要的邏輯以啟用/禁用按鈕。

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

只需一行解決方案。

我認為執行此操作的正確方法是將一個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