如何设置焦点EditText在Android中验证活动后?

时间:2013-12-23 06:05:49

标签: java android eclipse validation touch

如果用户输入无效的电子邮件或将其留空,我想显示Toast消息并将焦点设置为电子邮件文本字段。

3 个答案:

答案 0 :(得分:3)

试试这个,

EditText etEmailID = (EditText) layout.findViewById(R.id.etEmailID);
String emailID= etEmailID.getText().toString();
Context mContext = MainActivity.this; // Your_Context

if(emailID.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(emailID.matches())
  {
     Toast.makeText(mContext, "Please enter valid recipient address", Toast.LENGTH_LONG).show();
     etEmailID.requestFocus(emailID.length());
  }

希望它会对你有所帮助。

答案 1 :(得分:0)

要获得焦点并显示键盘,您可以编写如下内容:

 if(myEditText.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
 Toast.makeText(MainActivity.this, "your message to displayed on the screen", Toast.LENGTH_SHORT).show();
    }

答案 2 :(得分:0)

尝试使用此功能进行电子邮件验证..

EditText email = (EditText) findViewById(R.id.email);
    if(email.getText().toString().equals("")){
        email.requestFocus();
        Toast.makeText(getApplicationContext(), "please enter email", Toast.LENGTH_SHORT).show(); 
    }
    if(!eMailValidation(email.getText().toString())){
        email.requestFocus();
        Toast.makeText(getApplicationContext(), "please enter valid email", Toast.LENGTH_SHORT).show(); 
    }

eMailValidation(String email)是。

public static boolean eMailValidation(String email) {
    emailPattern = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}"
            + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
            + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");
    emailMatcher = emailPattern.matcher(email);
    return emailMatcher.matches();
}