实现多个按钮的单独选择,例
布局
<?xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<
ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="520dp"
android:maxHeight="520dp"
android:src="@drawable/s3"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:id="@+id/iv_image"
/>
<
RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/rg_radioGroup"
>
<
RadioButton
android:id="@+id/radioButton1"
android:layout_width="62dp"
android:layout_height="match_parent"
android:text="一号佳丽"
android:textSize="20dp"
android:onClick="select"
/>
<
RadioButton
android:id="@+id/radioButton2"
android:layout_width="62dp"
android:layout_height="match_parent"
android:text="二号佳丽"
android:textSize="20dp"
android:onClick="select"
/>
<
RadioButton
android:id="@+id/radioButton3"
android:layout_width="62dp"
android:layout_height="match_parent"
android:text="三号佳丽"
android:textSize="20dp"
android:onClick="select"
/>
<
RadioButton
android:id="@+id/radioButton4"
android:layout_width="62dp"
android:layout_height="match_parent"
android:text="四号佳丽"
android:textSize="20dp"
android:onClick="select"
/>
<
RadioButton
android:id="@+id/radioButton5"
android:layout_width="62dp"
android:layout_height="match_parent"
android:text="五号佳丽"
android:textSize="20dp"
android:onClick="select"
/>
<
RadioButton
android:id="@+id/radioButton6"
android:layout_width="62dp"
android:layout_height="match_parent"
android:text="六号佳丽"
android:textSize="20dp"
/>
</
RadioGroup>
</
LinearLayout>实现方法
public class RidioButtonActivity
extends AppCompatActivity{
private RadioGroup
radioGroup;
private Map<String, Integer>
h;
private ImageView
imageView;
@Override
protected void onCreate(
@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
radiobutton);
imageView = (ImageView) findViewById(R.id.
iv_image);
radioGroup = (RadioGroup) findViewById(R.id.
rg_radioGroup);
h =
new HashMap<>();
h.put(
"一号佳丽",R.drawable.
s1);
h.put(
"二号佳丽",R.drawable.
s2);
h.put(
"三号佳丽",R.drawable.
s3);
h.put(
"四号佳丽",R.drawable.
s4);
h.put(
"五号佳丽",R.drawable.
s5);
}
public void select(View view){
RadioButton radioButton=(RadioButton)view;
String name=radioButton.getText().toString();
imageView.setImageResource(
h.get(name));
}
}