本文介绍了在Android中使用SETERROR()与登记表验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我想点击提交按钮后提交it.So之前验证登记表,每个字段都有进行验证,并根据验证结果,如果没有正确填写,错误信息将是可见感谢SETERROR( )。但现在的问题是,还填补了正确的数据后,随着消息的错误图标仍然there.please帮助。

 这里是我的code为Signupform.xml文件: -     <滚动型
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@机器人:彩色/白>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=@机器人:彩色/白
        机器人:方向=垂直
        机器人:paddingBottom会=@扪/ activity_vertical_margin
        机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
        机器人:paddingTop =@扪/ activity_vertical_margin>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =中心
            机器人:文字=@字符串/注册
            机器人:文字颜色=#686868
            机器人:TEXTSIZE =25dp
            机器人:文字样式=大胆/>        <的EditText
            机器人:layout_width =match_parent
            机器人:layout_height =30dp
            机器人:layout_marginTop =20dp
            机器人:背景=@的mipmap / login_name_input
            机器人:提示=输入您的姓名
            机器人:MAXLINES =1
            安卓的inputType =TEXT
            机器人:ID =@ + ID / SignUp_name
            机器人:单线=真
            机器人:可聚焦=真
            机器人:可点击=真
            机器人:focusableInTouchMode =真/>        <的EditText
            机器人:layout_width =match_parent
            机器人:layout_height =30dp
            机器人:layout_marginTop =20dp
            机器人:背景=@的mipmap / login_name_input
            机器人:提示=请输入您的电子邮件地址
            安卓的inputType =textEmailAddress
            机器人:MAXLINES =1
            机器人:ID =@ + ID / SignUp_email
            机器人:单线=真
            机器人:可聚焦=真
            机器人:可点击=真
            机器人:focusableInTouchMode =真/>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =20dp
            机器人:文字=选择国家
            机器人:文字颜色=#676767/>        <微调
            机器人:layout_width =match_parent
            机器人:layout_height =30dp
            机器人:layout_marginTop =20dp
            机器人:项=@阵列/ COUNTRY_NAME>        < /微调>        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=横向>            <的EditText
                机器人:layout_width =0dp
                机器人:layout_height =30dp
                机器人:layout_marginTop =20dp
                机器人:layout_weight =1
                机器人:背景=@的mipmap / login_name_input/>            <的EditText
                机器人:layout_width =0dp
                机器人:layout_height =30dp
                机器人:layout_marginLeft =10dp
                机器人:layout_marginTop =20dp
                机器人:layout_weight =4
                机器人:背景=@的mipmap / login_name_input
                机器人:提示=请输入您的联系电话
                安卓的inputType =numberDecimal
                机器人:MAXLINES =1
                机器人:ID =@ + ID / SignUp_number
                机器人:单线=真
                机器人:可聚焦=真
                机器人:可点击=真
                机器人:focusableInTouchMode =真/>
        < / LinearLayout中>        <的EditText
            机器人:layout_width =match_parent
            机器人:layout_height =30dp
            机器人:layout_marginTop =20dp
            机器人:背景=@的mipmap / login_name_input
            机器人:提示=请输入密码
            安卓的inputType =textPassword
            机器人:ID =@ + ID / SignUp_password
            机器人:MAXLINES =1
            机器人:单线=真
            机器人:可聚焦=真
            机器人:可点击=真
            机器人:focusableInTouchMode =真/>        < ImageView的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =中心
            机器人:layout_marginTop =20dp
            机器人:背景=@的mipmap /的submit_btn
            安卓的inputType =TEXT
            机器人:文字=签署参加
            机器人:ID =@ + ID / SignUp_button
            机器人:文字颜色=@机器人:彩色/白
            机器人:文字样式=大胆/>
    < / LinearLayout中>
< /滚动型>
这里是我的code为SignupFragment.java:-
公共类SignUpFragment扩展片段实现View.OnClickListener {
    的EditText Signup_name,Signup_email,Signup_password,Signup_contact;
    ImageView的Signup_button;
    验证验证;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        //充气的布局该片段
        查看查看= inflater.inflate(R.layout.fragment_sign_up,集装箱,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(本);
        验证=新的验证();
        返回视图。
    }
    @覆盖
    公共无效的onClick(视图v)
    {
        如果(!validation.isValidName(Signup_name.toString()))
        {            Signup_name.setError(请输入名称);
        }
        如果(!validation.isValidEmail(Signup_email.toString()))
        {            Signup_email.setError(请输入有效的电子邮件地址);
        }
        如果(!validation.isValidEmail(Signup_password.toString()))
        {
            Signup_password.setError(最小密码长度必须6);
        }
        如果(!validation.isValidNumber(Signup_contact.toString()))
        {            Signup_contact.setError(请输入有效的联系电话);
        }
    }
}
这里是我的Validation.java code: -     ublic级验证
{
    布尔isValidEmail(字符串email){
        字符串EMAIL_PATTERN =^ [_ A-ZA-Z0-9 - \\\\ +] +(\\\\ [_ A-ZA-Z0-9 - ] +)* @
                +[A-ZA-Z0-9 - ] +(\\\\。[A-ZA-Z0-9] +)*(\\\\。[A-ZA-Z] {2})$;        模式模式= Pattern.compile(EMAIL_PATTERN);
        匹配器匹配= pattern.matcher(电子邮件);
        返回matcher.matches();
    }
    布尔isValidPassword(字符串传递){
        如果(pass.isEmpty()及!&放大器; pass.length()> = 6){
            返回true;
        }
        返回false;
    }
    布尔isValidNumber(字符串NUM)
    {
        如果(num.isEmpty()及!&放大器; num.length()== 10){
            返回true;
        }
        返回false;
    }
    布尔isValidName(字符串名称)
    {
        如果(name.isEmpty()及!&放大器; name.length()小于30){
            返回true;
        }
        返回false;
    }}


解决方案

您必须添加其他段删除SETERROR像 SETERROR(空)

 公共类SignUpFragment扩展片段实现View.OnClickListener {
    的EditText Signup_name,Signup_email,Signup_password,Signup_contact;
    ImageView的Signup_button;
    验证验证;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        //充气的布局该片段
        查看查看= inflater.inflate(R.layout.fragment_sign_up,集装箱,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(本);
        验证=新的验证();
        返回视图。
    }
    @覆盖
    公共无效的onClick(视图v)
    {
        如果(!validation.isValidName(Signup_name.getText()。的toString()))
        {
            Signup_name.setError(请输入名称);
        }其他{
            Signup_name.setError(NULL);
        }        如果(!validation.isValidEmail(Signup_email.getText()。的toString()))
        {
            Signup_email.setError(请输入有效的电子邮件地址);
        }其他{
            Signup_email.setError(NULL);
        }        如果(!validation.isValidEmail(Signup_password.getText()。的toString()))
        {
            Signup_password.setError(最小密码长度必须6);
        }其他{
            Signup_password.setError(NULL);
        }        如果(!validation.isValidNumber(Signup_contact.getText()。的toString()))
        {
            Signup_contact.setError(请输入有效的联系电话);
        }其他{
            Signup_contact.setError(NULL);
        }
    }
}
这里是我的Validation.java code: -     ublic级验证
{
    布尔isValidEmail(字符串email){
        字符串EMAIL_PATTERN =^ [_ A-ZA-Z0-9 - \\\\ +] +(\\\\ [_ A-ZA-Z0-9 - ] +)* @
                +[A-ZA-Z0-9 - ] +(\\\\。[A-ZA-Z0-9] +)*(\\\\。[A-ZA-Z] {2})$;        模式模式= Pattern.compile(EMAIL_PATTERN);
        匹配器匹配= pattern.matcher(电子邮件);
        返回matcher.matches();
    }
    布尔isValidPassword(字符串传递){
        如果(pass.isEmpty()及!&放大器; pass.length()> = 6){
            返回true;
        }
        返回false;
    }
    布尔isValidNumber(字符串NUM)
    {
        如果(num.isEmpty()及!&放大器; num.length()== 10){
            返回true;
        }
        返回false;
    }
    布尔isValidName(字符串名称)
    {
        如果(name.isEmpty()及!&放大器; name.length()小于30){
            返回true;
        }
        返回false;
    }}

Actually I am trying to validate a registration form before submitting it.So after clicking the submit Button,Each field has to be validated and based on the validation results if not properly filled,an error message will be visible thanks to SetError().But here is the problem,After filling the correct data also,that error icon with the message is still there.please help.

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;
    }

}
解决方案

You have to add else segment to remove setError like 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;
    }

}

这篇关于在Android中使用SETERROR()与登记表验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:33