我正在开发一个Android应用程序注册页面(用java语言),其中包含13个字段。我已经对所有字段进行了验证,并且使用Toast消息正常工作。但我的要求是,如果任何字段引发一个Toast消息,那么该字段应该突出显示。这是我的示例代码
if (driverName.length() <= 0) {
Toast.makeText(ApplicationActivity.this, "Enter first name", Toast.LENGTH_LONG).show();
} else if (firname) {
Toast.makeText(ApplicationActivity.this, "please enter the first name correctly", Toast.LENGTH_LONG).show();
} else if (driverName_last.length() <= 0) {
Toast.makeText(ApplicationActivity.this, "Enter last name", Toast.LENGTH_LONG).show();
} else if (secname) {
Toast.makeText(ApplicationActivity.this, "please enter last name correctly", Toast.LENGTH_LONG).show();
} else if (fatherName.length() <= 0) {
Toast.makeText(ApplicationActivity.this, "Enter father name", Toast.LENGTH_LONG).show();
} else if (fathername) {
Toast.makeText(ApplicationActivity.this, "please enter father name correctly", Toast.LENGTH_LONG).show();
}
提前致谢
答案 0 :(得分:2)
您可以使用setError()方法,而不是使用Toast。
input.setError("Your particular error");
其中,输入是 EditText 。
当if条件错误或根据您给定的条件时,它会将错误设置为特定的EditText。
它比显示Toast更好。
用代码编辑:
if (!Common.isValidLength(fName)) {
medFirstName.setError("Invalid First Name");
}
if (!Common.isValidLength(lName)) {
medLastName.setError("Invalid Last Name");
}
if (!Common.isValidEmail(email)) {
medEmailId.setError("Invalid Email");
}
if (!Common.isValidPassword(pass)) {
medPassword.setError("Invalid Password");
}
if (!Common.isValidPassword(confirmPassword)) {
medConfirmPassword.setError("Invalid Confirm Password");
}
if (!Common.isValidMatchPassword(pass, confirmPassword)) {
medConfirmPassword.setError("Password does not match");
}
为此创建一个Common类并在其中放入以下方法:
/*
* A Common function to check internet connection.
* */
public static boolean isOnline(Context c) {
try {
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/*
* A common function to check length for input.
* */
public static boolean isValidLength(String fName) {
if (fName.trim().length() > 0) {
return true;
}
return false;
}
/*
* A common function to validate Email id.
* */
public static boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
// validating password with retype password
public static boolean isValidPassword(String password) {
if (password != null) {
return true;
}
return false;
}
// validating of confirm password
public static boolean isValidMatchPassword(String pass, String confirmPassword) {
if (pass.equals(confirmPassword)) {
return true;
}
return false;
}
答案 1 :(得分:0)
要高亮区域,你必须设置例如。
firname.requestFocus();
注意:使用您的编辑文字名称更改firname
。
答案 2 :(得分:0)
requestFocus()方法将焦点返回到调用它的视图。
例如
else if (firname) {
Toast.makeText(ApplicationActivity.this, "please enter the first name correctly", Toast.LENGTH_LONG).show();
firname.requestFoucs(); ****// here firname is edittext****
}