Android片段中的单选按钮验证

时间:2018-09-28 04:14:59

标签: android

在我的程序中,我正在创建一个用户配置文件片段,其中包括名,姓,性别,出生日期,地址1,地址2和手机号码。即使在单选按钮中进行了选择,我在“性别”字段中也得到了空值。并且我收到了“请填写性别字段”的警报。 我是android编程的新手。帮我解决这个问题。 这是我的代码。

package com.example.aparna.listfragment.fragments;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
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;
import java.util.Locale;

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_mob;
    public ImageView img_same,img_calender;
    public Button btn_save;
    public RadioGroup radioGroup;
    public RadioButton radiobtn_male,radiobtn_female,rb;
    public DbCreate create;
    public String firstname,lastname,saddress1,saddress2,mobile_no,gender,date_of_birth;
    public int mYear,mMonth,mDay;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_profile, container, false);
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        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);



        // date picker
        img_calender.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                final Calendar mcurrentDate=Calendar.getInstance();
                mYear=mcurrentDate.get(Calendar.YEAR);
                mMonth=mcurrentDate.get(Calendar.MONTH);
                mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                        tv_setdate.setText(selectedday+"/"+selectedmonth+"/"+selectedyear);
                        date_of_birth=tv_setdate.getText().toString();
                        }
                },mYear,mMonth,mDay);
                mDatePicker.show();  }
        });



        //if address is same
        img_same.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edittext_address2.setText(edittext_address1.getText().toString());
                saddress2=saddress1;
                edittext_address2.setSelection(edittext_address2.getText().length());//moving cursor to the end of edit text

            }
        });






        // 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) {
                       RadioButton rb= (RadioButton)group.findViewById(checkedId);
                        gender=rb.getText().toString();
                        Log.e("gender","=="+gender );
                    }
                });





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


                 mobile_no=edittext_mob.getText().toString();
                Log.e("mobile no ","=="+mobile_no);


//----------------------------------------------------------------------------------------------------

                if(firstname.isEmpty()) {
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the firstname field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

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


                    else if(lastname.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the lastname field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                  }

                else if( !lastname.matches("[a-zA-Z]+")) {
                    edittext_lastname.setError("ENTER ONLY ALPHABETICAL CHARACTER");
                }


                else if(gender.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the gender field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if(date_of_birth.isEmpty() ){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the dob field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if( saddress1.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the address1 field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }
                else if(saddress2.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the address2 field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }

                else if(mobile_no.isEmpty()){
                    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                    alert.setTitle("ALERT");
                    alert.setIcon(R.drawable.ic_warning_black_24dp);
                    alert.setMessage("Please fill the mobile no field");
                    alert.setPositiveButton("ok",null);
                    alert.show();
                }
                else if( mobile_no.length()!=10){
                    edittext_mob.setError("ENTER VALID NUMBER");

                }

                else{
                    create.insertUser(firstname,lastname,gender,date_of_birth,saddress1,saddress2,mobile_no);
                    //create.deleteAll();
                    Toast.makeText(getActivity(), "data saved", Toast.LENGTH_SHORT).show();
                    }

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

        return view;
    }

}

1 个答案:

答案 0 :(得分:0)

请更改此

   RadioButton rb= (RadioButton)group.findViewById(checkedId);

对此:

将其粘贴到OnCreatedView中,从当前代码中从OnClick中删除

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                   RadioButton rb= (RadioButton)view.findViewById(checkedId);
                    gender=rb.getText().toString();
                    Log.e("gender","=="+gender );
                }
            });

如果要在Button上单击,请按照以下步骤

在OnCreatedView中查找findViewById

 radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);

在“按钮单击”中使用此按钮:

        int selectedId=radioSexGroup.getCheckedRadioButtonId();
        radioSexButton=(RadioButton)findViewById(selectedId);

在片段中:

  RadioButton rb= (RadioButton)view.findViewById(checkedId);