我的要求是使用count.when我们按三次按钮我想要出现toast消息为Blocked ??请任何人都可以显示代码或条件。编程中有任何错误?
public void empbtn(View v) {
EditText et11,et22;
int counter = 3;
int i;
String unames[] = {"Anil","Ramesh","Khaja"};
String passwds[] = {"anil","ramesh","khaja"};
et11 = (EditText)findViewById(R.id.empname);
et22 = (EditText)findViewById(R.id.emppass);
String uname = et11.getText().toString();
String pass = et22.getText().toString();
if(uname.equals(""))
{
et11.setError("PLS ENTER NAME");
}
else if (pass.equals(passwds))
{
//et22.setError("pls enter password");
Toast.makeText(main.this, "valid", Toast.LENGTH_SHORT).show();
}
else {
counter++;
if (counter > 3)
{
Toast.makeText(main.this, "blocked", Toast.LENGTH_SHORT).show();
}
else {
for (i = 0; i < unames.length; i++) {
if (uname.equals(unames[i]) && pass.equals(passwds[i])) {
setContentView(R.layout.home);
TextView tv = (TextView) findViewById(R.id.names);
tv.setText(uname);
break;
}
}
if (i == unames.length) {
Toast.makeText(main.this, "INVALID", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
您可以将布尔值作为标志来知道何时有效:
boolean isValid = false;
for (i = 0; i < unames.length; i++) {
if (uname.equals(unames[i]) && pass.equals(passwds[i])) {
setContentView(R.layout.home);
TextView tv = (TextView) findViewById(R.id.names);
tv.setText(uname);
isValid=true
break;
}
}
if (!isValid) {
Toast.makeText(main.this, "INVALID", Toast.LENGTH_SHORT).show();
}
修改强>
试试这个:
if (i == unames.length-1) {
Toast.makeText(main.this, "INVALID", Toast.LENGTH_SHORT).show();
}