安卓单选组件使用

xml:


<RadioGroup android:id="@+id/group1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="radio1"/>
    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="radio2"/>
    <RadioButton
            android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
            android:text="radio3"/>
</RadioGroup>
<TextView android:text="你的选择是:"
          android:id="@+id/txtResult"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>

java中的代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView textView=(TextView)findViewById(R.id.txtResult);
        RadioGroup radioGroup=(RadioGroup)findViewById(R.id.group1);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int radioButtonId=radioGroup.getCheckedRadioButtonId();
                RadioButton rb=(RadioButton)helloworld.this.findViewById(radioButtonId);

                textView.setText(rb.getText());
            }
        });
    }

最终效果:

 

 
posted @ 2015-11-02 22:26  carsick_cars  阅读(439)  评论(0编辑  收藏  举报