本文介紹了根據Android中EditText中的文字啟用和禁用Button的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如果 EditText 中的單詞少于 3 個單詞,我想通過 Button 禁用,如果 EditText 中的單詞超過 3 個單詞,那么我想啟用它以便它可以被點擊.
I want to disable by Button if the words in the EditText is less than 3 words, and if the words in the EditText are more than 3 words then I want to enable it so that it can get Clicked.
有人可以幫我嗎?
推薦答案
你必須 addTextChangedListener
到你的 EditText
像這樣:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
enableSubmitIfReady();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
在那個方法中,你應該這樣做:
In that method, you should do like this:
public void enableSubmitIfReady() {
boolean isReady = yourEditText.getText().toString().length() > 3;
yourbutton.setEnabled(isReady);
}
希望對你有幫助.
這篇關于根據Android中EditText中的文字啟用和禁用Button的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!