如果EditText
爲空,則必須禁用登錄Button
。如果EditText
有一些文字,則必須啓用登錄Button
。那麼你可以在Instagram上看到這種方法。在Android中,如何針對EditText使Login按鈕禁用?
這兩個字段都是空的,請登錄Button
已被禁用。
這裏密碼字段爲空,所以還是請登錄Button
被禁用。
這裏既有用戶名和密碼字段不爲空,所以登錄Button
被啓用。
如何實現這些步驟? 這裏是我的代碼,它不工作..
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_check);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
Button b = (Button) findViewById(R.id.button1);
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("")|| s2.equals("")){
b.setEnabled(false);
} else {
b.setEnabled(true);
}
}
嘗試尋找在文本實用程序:http://developer.android.com/reference/android/text/TextUtils.html - 它有一個「isEmpty」功能,可能在這裏很方便。 – Evilunclebill 2013-02-21 13:08:07
你試過這個if(s1.length()== 0 || s2.length()== 0)? – 2013-02-21 13:08:28
檢查我的答案,如果它適合你。 – moDev 2013-02-21 13:48:35