android----RadioGroup

XML代码:

android----RadioGroup
<RadioGroup 
        android:id="@+id/sexGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     //设置RadioGroup中的RadioButton是水平布局还是垂直布局 android:orientation="vertical"> <RadioButton android:id="@+id/manRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:checked="true" android:text="@string/manRadio" /> <RadioButton android:id="@+id/womanRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/manRadio" android:layout_below="@id/manRadio" android:layout_marginTop="18dp" android:text="@string/womanRadio" /> </RadioGroup>
android----RadioGroup

java代码:

android----RadioGroup
manRadio=(RadioButton)findViewById(R.id.manRadio);
        womanRadio=(RadioButton)findViewById(R.id.womanRadio);
        sexGroup=(RadioGroup)findViewById(R.id.sexGroup);
        sexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
          //通过id判断用户选择的是哪个RadioButton
if(R.id.manRadio==checkedId){ Toast.makeText(MainActivity.this, "你选择了--->>男", 2).show(); }else{ Toast.makeText(MainActivity.this, "你选择了--->>女", 2).show(); } } });
android----RadioGroup

android----RadioGroup

上一篇:一致性Hash


下一篇:android----ToggleButton&Switch