简体   繁体   中英

How to select one radio button in Android

  1. Here is my xml view
  2. Within the horizontalScrollView I have images and radio button.
  3. I want to select only one radio button.But here I can select more than one radio button. Please help me how to do that.

      <HorizontalScrollView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" >

      <LinearLayout
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

             <LinearLayout
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:orientation="vertical" >

              <ImageView
                android:id="@+id/partly"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               />

          <RadioButton
              android:id="@+id/radioButton1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />

        </LinearLayout>


             <LinearLayout>

              <ImageView../>

              <RadioButton../>

             </LinearLayout>


       </LinearLayout>

       </HorizontalScrollView>

What I understand from your xml code is that you want something scrollable views out of which only one can be selected. For which you are using RadioButton which is usually used to force user to select only one item.

You can achieve such functionality with radio buttons using RadioGroup.

But again if you use RadioGroup it can holde only RadioButtons as a child. While your case is different. You have LinearLayout as a child of ScrollView and RadioButton is a child of that LinearLayout

What you should do

1) Set some radio button checked as a default. And assign its index to some variable ie activePosition

2) Now When user taps on some other radio button you should set checked property that old RadioButton as false and change the activePosition=currentlyTappedRadioButton's view's position

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM