验证Android片段中的手机号码

时间:2018-09-27 05:02:31

标签: android

我正在创建一个包含firstname, lastname ,gender,date of birth,address1,address2 and mobile number的用户个人资料片段。

在这里,我的程序无法验证电话号码。即使我在手机号码字段中输入了10位数字,也会出现输入有效号码的错误。

这是我的代码。

package com.example.aparna.listfragment.fragments;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.aparna.listfragment.R;
import com.example.aparna.listfragment.database.DbCreate;

import java.text.SimpleDateFormat;
import java.util.Calendar;


public class ProfileFragment extends Fragment {
    public TextView tv_firstname,tv_lastname,tv_dob,tv_setdate,tv_address1,tv_address2,tv_mob;
    public EditText edittext_firstname,edittext_lastname,edittext_address1,edittext_address2,edittext_selectdate,edittext_mob;
    public ImageView img_same,img_calender;
    public Button btn_save;
    public String firstname,lastname,saddress1,saddress2,mobile_no;
    //public int mob;
    public RadioGroup radioGroup;
    public RadioButton radiobtn_male,radiobtn_female,rb;
    public DbCreate create;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_profile, container, false);

        tv_firstname=(TextView)view.findViewById(R.id.tv_firstname);
        edittext_firstname=(EditText) view.findViewById(R.id.edittext_firstname);
        tv_lastname=(TextView)view.findViewById(R.id.tv_lastname);
        edittext_lastname=(EditText) view.findViewById(R.id.edittext_lastname);

        tv_address1=(TextView)view.findViewById(R.id.tv_address1);
        edittext_address1=(EditText)view.findViewById(R.id.edittext_address1);
        tv_address2=(TextView)view.findViewById(R.id.tv_address2);
        edittext_address2=(EditText)view.findViewById(R.id.edittext_address2);

        radioGroup=(RadioGroup)view.findViewById(R.id.radioGroup);
        btn_save=(Button)view.findViewById(R.id.btn_save);

        tv_dob=(TextView)view.findViewById(R.id.tv_dob);
        tv_setdate=(TextView) view.findViewById(R.id.tv_setdate);

        img_same=view.findViewById(R.id.img_same);
        img_calender=view.findViewById(R.id.img_calender);

        tv_dob=(TextView)view.findViewById(R.id.tv_mob);
        edittext_mob=(EditText) view.findViewById(R.id.edittext_mob);


        // save button pressed
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                create=new DbCreate(getActivity());

                firstname=edittext_firstname.getText().toString();
                lastname=edittext_lastname.getText().toString();


                //radio button
                radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
                {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        rb= group.findViewById(checkedId);
                        Log.e("the button","selected is"+rb .getText());
                    }
                });


                saddress1=edittext_address1.getText().toString();
                saddress2=edittext_address2.getText().toString();

                //if address is same
                img_same.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("data entered","here" );
                        edittext_address2.setText(edittext_address1.getText().toString());
                        edittext_address2.setSelection(edittext_address2.getText().length());//moving cursor to the end of edit text

                        }
                });



                //date picker

//                img_calender.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        DialogFragment newFragment = new DatePickerFragment();
//                        newFragment.show(getFragmentManager(), "datePicker");
//                    }
//                });

                final String mobile_no=edittext_mob.getText().toString();
                Log.e("mobile no ","=="+mobile_no.length() );
                final int mob=Integer.parseInt(mobile_no);
               // Log.e("int mobile no ", "=="+mob);


                if(firstname.isEmpty() || lastname.isEmpty()|| radioGroup.getCheckedRadioButtonId()==-1|| saddress1.isEmpty() || saddress2.isEmpty() || mobile_no.isEmpty() ) {
                    Log.e("data entered","here" );
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setMessage("Please fill all the fields");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

              else if(!firstname.matches("[a-zA-Z ]+") ) {
                    edittext_firstname.requestFocus();
                    edittext_firstname.setError("ENTER ONLY ALPHABETICAL CHARACTER");

                    }

                else if( !lastname.matches("[a-zA-Z]+")) {

                    edittext_lastname.requestFocus();
                    edittext_lastname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
                }

                else if(!mobile_no.matches("[a-zA-Z]+")|| mobile_no.length()!=10){
                    edittext_mob.setError("ENTER VALID NUMBER");

                }

                else{
                    create.insertUser(firstname,lastname);
                    Toast.makeText(getActivity(), "data saved", Toast.LENGTH_SHORT).show();
                    }

                Log.e("no. of","data=="+create.userCount() );
                }
        });

        return view;
    }
}

4 个答案:

答案 0 :(得分:4)

删除此条件->

else if(!mobile_no.matches("[a-zA-Z]+")|| mobile_no.length()!=10){
                edittext_mob.setError("ENTER VALID NUMBER");
            }

并对其进行修改:

else if(mobile_no.length()!=10){
                edittext_mob.setError("ENTER VALID NUMBER");
            }

并在您初始化此editText的地方定义提及的那一行->

mobile_no.setInputType(InputType.TYPE_CLASS_NUMBER);

答案 1 :(得分:0)

更改

if(!mobile_no.matches("[a-zA-Z]+")|| mobile_no.length()!=10)

if(!mobile_no.matches("\\d+")|| mobile_no.length()!=10)

答案 2 :(得分:0)

为什么要检查字符模式?试试这个

if(!(mobileNo.matches("\\d+")) || mobileNo.length != 10){
// Your message here
}

答案 3 :(得分:0)

 else if(!mobile_no.matches("[a-zA-Z]+")|| mobile_no.length()!=10){
                    edittext_mob.setError("ENTER VALID NUMBER");

                }  

此条件表明,如果mobile_no与“ a-z”或“ A-Z”不匹配,或者mobile_no.length不等于10,则显示错误
尝试删除“!”处于第一状态

else if(mobile_no.matches("[a-zA-Z]+")|| mobile_no.length()!=10){
                edittext_mob.setError("ENTER VALID NUMBER");

            }