我有一个注册表单,要求用户输入两次用户名和密码,我想不提交表单并提醒用户,如果他混淆填写任何字段,但按钮不是在所有情况下工作,这是我的代码:
user = (EditText) this.findViewById(R.id.username);
pass = (EditText) this.findViewById(R.id.password);
pass2 = (EditText) this.findViewById(R.id.password2);
u = user.getText().toString();
p1 = pass.getText().toString();
p2 = pass2.getText().toString();
if(!(u.equals("")||p1.equals("")||p2.equals(""))){
btn = (Button) this.findViewById(R.id.register2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!p1.equals(p2))
Toast.makeText(getApplicationContext(), "Passwords didn't match!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "Passwords match!", Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:3)
删除此if条件行
if(!(u.equals("")||p1.equals("")||p2.equals(""))){
并将其放入点击lisner
if(!(u.equals("")||p1.equals("")||p2.equals(""))){
//Toast message please enter the username or pwd
return;
}
答案 1 :(得分:1)
我发现理想的答案是制作如下方法:
boolean isEmpty(EditText e){
return e.getText().toString().trim().length() == 0;
}
答案 2 :(得分:1)
简单的方法是检查EditText框内容的长度
EditText resultInput = (EditText)findViewById(R.id.result);
if(resultInput.getText().length() > 0)
// Edit Text is Empty
else
// Edit Text is Empty
答案 3 :(得分:0)
if(!(user.getText()。length()< = 0)&(pass.getText()。length()< = 0)&(pass2.getText()。length() < = 0))){ btn =(Button)this.findViewById(R.id.register2); ...