实际上我正在尝试在提交之前验证注册表单。因此,在单击提交按钮后,必须验证每个字段并根据验证结果如果未正确填充,由于SetError,将显示错误消息( )。但这里是问题,在填写正确的数据之后,带有消息的错误图标仍然存在。请帮助。
here is my code for the Signupform.xml file:-
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/Signup"
android:textColor="#686868"
android:textSize="25dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:background="@mipmap/login_name_input"
android:hint="Enter your name"
android:maxLines="1"
android:inputType="text"
android:id="@+id/SignUp_name"
android:singleLine="true"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:background="@mipmap/login_name_input"
android:hint="Enter your Email Address"
android:inputType="textEmailAddress"
android:maxLines="1"
android:id="@+id/SignUp_email"
android:singleLine="true"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Select your Country"
android:textColor="#676767" />
<Spinner
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:entries="@array/Country_name">
</Spinner>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="@mipmap/login_name_input" />
<EditText
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:background="@mipmap/login_name_input"
android:hint="Enter your Contact Number"
android:inputType="numberDecimal"
android:maxLines="1"
android:id="@+id/SignUp_number"
android:singleLine="true"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:background="@mipmap/login_name_input"
android:hint="Enter your password"
android:inputType="textPassword"
android:id="@+id/SignUp_password"
android:maxLines="1"
android:singleLine="true"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@mipmap/submit_btn"
android:inputType="text"
android:text="Sign Me Up"
android:id="@+id/SignUp_button"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
Here is my code for the SignupFragment.java:-
public class SignUpFragment extends Fragment implements View.OnClickListener {
EditText Signup_name,Signup_email,Signup_password,Signup_contact;
ImageView Signup_button;
Validation validation;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_sign_up, container, false);
Signup_name=(EditText)view.findViewById(R.id.SignUp_name);
Signup_email=(EditText)view.findViewById(R.id.SignUp_email);
Signup_password=(EditText)view.findViewById(R.id.SignUp_password);
Signup_contact=(EditText)view.findViewById(R.id.SignUp_number);
Signup_button=(ImageView)view.findViewById(R.id.SignUp_button);
Signup_button.setOnClickListener(this);
validation=new Validation();
return view;
}
@Override
public void onClick(View v)
{
if(!validation.isValidName(Signup_name.toString()))
{
Signup_name.setError("please enter a name");
}
if(!validation.isValidEmail(Signup_email.toString()))
{
Signup_email.setError("please enter a Valid Email Address");
}
if(!validation.isValidEmail(Signup_password.toString()))
{
Signup_password.setError("min password length has to be 6");
}
if(!validation.isValidNumber(Signup_contact.toString()))
{
Signup_contact.setError("please enter a Valid Contact number");
}
}
}
Here is my Validation.java code:-
ublic class Validation
{
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();
}
boolean isValidPassword(String pass) {
if (!pass.isEmpty() && pass.length() >= 6) {
return true;
}
return false;
}
boolean isValidNumber(String num)
{
if (!num.isEmpty() && num.length()==10) {
return true;
}
return false;
}
boolean isValidName(String name)
{
if (!name.isEmpty() && name.length()<30) {
return true;
}
return false;
}
}
答案 0 :(得分:3)
您必须添加else段以删除setError,例如setError(null)
public class SignUpFragment extends Fragment implements View.OnClickListener {
EditText Signup_name,Signup_email,Signup_password,Signup_contact;
ImageView Signup_button;
Validation validation;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_sign_up, container, false);
Signup_name=(EditText)view.findViewById(R.id.SignUp_name);
Signup_email=(EditText)view.findViewById(R.id.SignUp_email);
Signup_password=(EditText)view.findViewById(R.id.SignUp_password);
Signup_contact=(EditText)view.findViewById(R.id.SignUp_number);
Signup_button=(ImageView)view.findViewById(R.id.SignUp_button);
Signup_button.setOnClickListener(this);
validation=new Validation();
return view;
}
@Override
public void onClick(View v)
{
if(!validation.isValidName(Signup_name.getText().toString()))
{
Signup_name.setError("please enter a name");
} else {
Signup_name.setError(null);
}
if(!validation.isValidEmail(Signup_email.getText().toString()))
{
Signup_email.setError("please enter a Valid Email Address");
} else {
Signup_email.setError(null);
}
if(!validation.isValidEmail(Signup_password.getText().toString()))
{
Signup_password.setError("min password length has to be 6");
} else {
Signup_password.setError(null);
}
if(!validation.isValidNumber(Signup_contact.getText().toString()))
{
Signup_contact.setError("please enter a Valid Contact number");
} else {
Signup_contact.setError(null);
}
}
}
Here is my Validation.java code:-
ublic class Validation
{
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();
}
boolean isValidPassword(String pass) {
if (!pass.isEmpty() && pass.length() >= 6) {
return true;
}
return false;
}
boolean isValidNumber(String num)
{
if (!num.isEmpty() && num.length()==10) {
return true;
}
return false;
}
boolean isValidName(String name)
{
if (!name.isEmpty() && name.length()<30) {
return true;
}
return false;
}
}
答案 1 :(得分:0)
private EditText edt_firstName,edt_email;
private String firstName,email;
edt_firstName = findViewById(R.id.edt_firstName);
edt_email = findViewById(R.id.edt_email);
private void validateData(){
firstName = edt_firstName.getText().toString().trim();
email = edt_email.getText().toString().trim();
if (!firstName.isEmpty() && !email.isEmpty() && isValidEmail(email) {
//here api call of the register user.
}else{
if (firstName.isEmpty()) {
edt_firstName.setError("Please Enter First Name");
edt_firstName.requestFocus();
}
else if (email.isEmpty()) {
edt_email.setError("Please Valid Email Id");
edt_email.requestFocus();
} else if (!isValidEmail(email)) {
edt_email.setError("Please Enter Valid Email");
edt_email.requestFocus();
}
}
}
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches();
}