[英]How to select one radio button in Android
<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>
我从您的xml代码中了解到,您想要一些可滚动的视图,只能从其中选择一个。 为此,您正在使用RadioButton,该按钮通常用于强制用户仅选择一项。
您可以使用RadioGroup通过单选按钮实现此类功能。
但是同样,如果您使用RadioGroup,它只能将RadioButtons作为子级。 虽然您的情况有所不同。 您将LinearLayout作为ScrollView的子级,而RadioButton是该LinearLayout的子级
你应该怎么做
1)将某些单选按钮设置为默认选中状态。 并将其索引分配给某个变量,即activePosition
2)现在,当用户点击其他单选按钮时,您应该将旧RadioButton的选中属性设置为false,并更改activePosition = currentlyTappedRadioButton的视图位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.