这是问题显示登录表单;当用户将两个文本框留空时,应用程序必须生成一个消息框,通知用户“请填写必填字段”,同时单击登录按钮;其中,生成的消息框弹出并通知用户“密码错误!”,当用户输入正确的用户名和错误密码时,将自动清除密码文本框;其中应用程序必须生成一个消息框,通知用户“用户名错误!”如果用户输入错误的用户名和密码,将自动清除用户名文本框;其中应用程序必须生成一个消息框,通知用户“错误的用户名和密码!”如果用户输入错误的用户名和错误的密码,它将自动清除密码和用户名文本框,并将文本焦点设置为用户名文本框;其中; app必须通知用户“WELCOME”,如果用户输入正确的用户名和密码,它将自动关闭登录活动并打开下一个活动 . 注意:用户名和密码必须都是“admin” .
Button login;
EditText user, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
user = (EditText)findViewById(R.id.editText1 );
pass = (EditText)findViewById(R.id.editText2 );
login = (Button)findViewById(R.id.button1 );
login.setOnClickListener((OnClickListener) this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(user.equals("admin") && (pass.equals("admin"))){
System.out.println("Welcome!");
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}else if (user.equals(null) && (pass.equals("admin"))) {
System.out.println ("Please Complete Required Field!");
}else if (user.equals("admin") && (pass.equals(null))) {
System.out.println ("Please Complete Required Field!");
}else {
System.out.println ("Wrong Username! or Wrong Password!");
}
}
}
3 回答
To detect whether the editText is empty or not:
将
(user.equals(null))
更改为user.getText().toString().trim().length() == 0
和
pass.equals(null)
至pass.getText().toString().trim().length() == 0
在你的按钮实现中,写成
login.setOnClickListener(this)
;和@853863_一样@ pcg26说 .确保已实现onClickListener . 在onClick方法中,必须使用 switch 或 if 语句来确保单击该按钮 . 像这样的东西:
该活动是否实施onclicklistener?
简单评论..您可以使用
Log.e("MyActivity"," the error");
代替System.out.println ^^