即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

ImageView、RadioGroup、RadioButton、button

编程语言 umbrella_419 13℃ 0评论

实现多个按钮的单独选择,例


布局
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 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));
    }
}




转载请注明:CodingBlog » ImageView、RadioGroup、RadioButton、button

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情