我有两个用户名和密码的edittexts,我使用seterror方法在两者都为空时显示错误。问题是当第二个字段(密码字段)中弹出错误消息时,部分消息丢失。这个bug只存在于较旧的设备上。如何确保旧设备中不会发生错误。
我的代码:
public class SignInPage extends Activity {
EditText txtusername,txtpassword;
Button btnlogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signinpage);
txtusername=(EditText) findViewById(R.id.txtusername);
txtpassword=(EditText) findViewById(R.id.txtpassword);
btnlogin=(Button) findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(txtusername.getText().toString().trim().equals(""))
{
txtusername.setError("Username is mandatory");
txtusername.requestFocus();
}
if(txtpassword.getText().toString().trim().equals(""))
{
txtpassword.setError("Password is mandatory");
txtpassword.requestFocus();
}
else
{
Toast.makeText(getApplicationContext(),"Checking with server",Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
试试这可能会对你有所帮助
fname = FirstName.getText().toString().trim();
if(fname.isEmpty()) //&& fname.matches("[a-zA-Z ]+"))
{
FirstName.requestFocus();
FirstName.setError(Html.fromHtml("<font color='red'>Please enter the First name</font>"));
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
@Override
public void afterTextChanged(Editable arg0)
{
if(arg0.toString().isEmpty())
{
return;
}
if (FirstName.getText() == arg0) {
Validate(FirstName);
FirstName.addTextChangedListener(textWatcher);
public void Validate(EditText et) {
et.setError(null);
}