【发布时间】:2014-01-16 15:51:03
【问题描述】:
我有一些代码,如果 editText 字段中的字符少于 3 个,我需要禁用“创建帐户”按钮。如果用户输入 3 个字符,则该按钮应启用自身以便可以使用。
我已经构建了 if else 语句,如果 editText 字段中的字符少于 3 个,则禁用按钮,但是在输入时,当用户插入 3 个字符时,它不会重新评估该语句是否为真所以按钮当然会保持禁用状态。
一旦用户在编辑文本字段中输入 3 个字符,按钮应该会自行启用。
Button buttonGenerate = (Button) findViewById(R.id.btnOpenAccountCreate);
userInitials = (EditText) findViewById(R.id.etUserChar);
if (userInitials.getText().toString().length() > 3) {
// Account Generator Button
buttonGenerate.setEnabled(true); // enable button;
buttonGenerate.setOnClickListener(new OnClickListener() {
//Do cool stuff here
@Override
public void onClick(View v) {
}
});// END BUTTON
} else {
// If UserInitials is empty, disable button
Toast.makeText(this, "Please enter three(3) characters in the Initials Field ", Toast.LENGTH_LONG)
.show();
buttonGenerate.setEnabled(false); // disable button;
}// END IF ELSE
【问题讨论】:
标签: java android button android-edittext