我有两个EditText,如何在右侧的EditText中设置光标
EditText emailE = (EditText) findViewById(R.id.editTextEmailLogin);
EditText passwordE = (EditText)findViewById(R.id.editTextPasswordLogin);
String email = emailE.getText().toString().trim();
String password = passwordE.getText().toString().trim();
如果用户按下Singin并且电子邮件EditText为空,请将光标设置在电子邮件EditText中.
密码EditText也一样
if (TextUtils.isEmpty(email)){
Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
//set cursor in Email editText
emailE.setSelection(0);
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
//set cursor in Password editText
passwordE.setSelection(0);
return;
}
解决方法:
你可以使用requestFocus();像这样的edittext的方法
if (TextUtils.isEmpty(email)){
Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
//set cursor in Email editText
emailE.requestFocus();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
//set cursor in Password editText
passwordE.requestFocus();
return;
}