Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

android - Reset a Radio button inside a Radio group

How I reset a radio button inside a radio group like the image example below

All my radio groups and its radio buttons are created programmatically.

I tried to to set OnClickListener to get the radio button value before getting changed but, it didn't help.

enter image description here

Edit: I posted the answer below, please check it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this :

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.radioButton){
                 //do something     
                }
                else if(checkedId == R.id.radioButton2){

                }
                else{

                }
        });

xml layout

<RadioGroup
      android:layout_width="fill_parent"
      android:layout_height="90dp"
      android:layout_below="@+id/imageView"
      android:layout_marginTop="58dp"
      android:weightSum="1"
      android:id="@+id/radioGroup"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_alignRight="@+id/textView3"
      android:layout_alignEnd="@+id/textView3">

      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="55dp"
         android:text="Male"
         android:id="@+id/radioButton"
         android:layout_gravity="center_horizontal"
         android:checked="false"
         android:textSize="25dp" />

      <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Female"
         android:id="@+id/radioButton2"
         android:layout_gravity="center_horizontal"
         android:checked="false"
         android:textSize="25dp"
         android:layout_weight="0.13" />
   </RadioGroup>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...