单选框RadioGroup-RadioButton

public class MainActivity extends Activity {
    private RadioGroup group_sex, group_grade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 找出对应的控件
        group_sex = (RadioGroup) findViewById(R.id.group_sex);
        group_grade = (RadioGroup) findViewById(R.id.group_grade);
    }
    // 1. public void 属性
    // 2. 参数为View对象
    // 点击提交按钮时调用该方法
    public void btn_click(View view) {
        // 找出RadioGroup中选中的RadioButton
        // 方法1. 使用RadioGroup的getCheckedRadioButtonId()找出选中的RadioButton的ID
        int id = group_sex.getCheckedRadioButtonId();
        RadioButton button = (RadioButton) findViewById(id);
        String str_sex = button.getText().toString();
        // 方法2. 遍历RadioGroup下的所有RadioButton 找出选中的项
        String str_grade = "";
        for(int i = 0; i < group_grade.getChildCount(); i++) {
            RadioButton radioButton = (RadioButton) group_grade.getChildAt(i);
            if(radioButton.isChecked()) {
                str_grade = radioButton.getText().toString();
                break;
            }
        }
        Toast.makeText(MainActivity.this, str_sex+" " + str_grade, Toast.LENGTH_SHORT).show();
    }
}

 

 //状态改变才会调用,如果在布局中默认设置了选择哪个,也不会执行该监听,毕竟是在布局中设置后才加载的
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                Log.i("tag", "onCheckedChanged: "+radioButton.getText().toString());
            }
        });

xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.day04_radiobutton.MainActivity" >

    <!-- RadioGroup进行分组 一组中多个RadioButton只能选一个
         android:orientation只能RadioGroup中RadioButton的排列方向
                 vertical垂直(默认)
                 horizontal 水平 -->

    <RadioGroup
        android:id="@+id/group_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <!-- RadioButton中 android:checked="true"表示默认选中 -->
        <RadioButton
            android:id="@+id/rb_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男" />

        <RadioButton
            android:id="@+id/rb_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/group_grade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rb_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大一" />

        <RadioButton
            android:id="@+id/rb_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大二" />

        <RadioButton
            android:id="@+id/rb_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大三" />

        <RadioButton
            android:id="@+id/rb_four"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大四" />
    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击确定"
        android:onClick="btn_click"
        />
</LinearLayout>

 =========================================================================================

单选框的一些其他用法:  如果需要为单选项添加图片(上图下文的样式),需要的方法就是android:drawableTop="@drawable/"

实现的效果如下:利用单选框实现(1000 500 200 100为4个单选按钮,被点击的为黄色)

1.在res文件夹下新建一个drawable文件夹,用来存放一些关于图片的xml文件:

 

2.布局文件如下:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ts.work.MainActivity" >

   <!-- 下面设置1000这个选项为默认选中的,如果4个单选框不设置id,被默认设置选中状态的按钮将不能被点击,
           也一直处于选中状态, 除非根据id在java代码中进行设置,用到的关键属性为 android:button="@null"  :使单选框没有选择框     android:drawableTop="@drawable/" 为单选框文本设置图片
            android:background="@drawable/selector" 将背景图片设置成为选择器-->
   <RadioGroup 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <RadioButton 
           android:layout_width="0dp"
           android:id="@+id/br1000"
           android:layout_height="wrap_content"
           android:text="1000"
           android:button="@null"
           android:gravity="center"
           android:checked="true"
           android:background="@drawable/selector"
           android:layout_weight="1"/>
       <RadioButton 
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:id="@+id/br500"
           android:text="500"
           android:button="@null"
           android:gravity="center"
           android:background="@drawable/selector"
           android:layout_weight="1"/>
       <RadioButton 
           android:layout_width="0dp"
            android:id="@+id/br200"
           android:layout_height="wrap_content"
           android:text="200"
           android:button="@null"
           android:gravity="center"
           android:background="@drawable/selector"
           android:layout_weight="1"/>
       <RadioButton 
           android:layout_width="0dp"
            android:id="@+id/br100"
           android:layout_height="wrap_content"
           android:text="100"
           android:button="@null"
           android:gravity="center"
           android:background="@drawable/selector"
           android:layout_weight="1"/>
   </RadioGroup>
   
</LinearLayout>

3.上面背景图的选择器的selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--state_checked 参数为true 代表选择的状态  -->
    <item android:state_checked="true" android:drawable="@drawable/color1"></item>
    <!--state_checked 参数为false 代表没有选择的状态  -->
    <item android:state_checked="false" android:drawable="@drawable/color2"></item>
</selector>

4.上面选择器用到的属性的color1与color2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="#ffff00">
</color>
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="#ff0000">
</color>

对于改变文字的颜色,这样设置就好了:  res/color/near_sex_rb_tv_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--布局中这样引用: android:textColor="@drawable/selector_textcolor"-->
    <item android:state_checked="true" android:color="@color/color1"/>
    <item android:state_checked="false" android:color="@color/color2"/>
</selector>

效果:

改变单选按钮选中与没选中的图片:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 选中与没选中的图片 -->
    <item android:state_checked="true" android:drawable="@drawable/icon_complaint_select"></item>
    <item android:state_checked="false" android:drawable="@drawable/icon_complaint_normal"></item>
</selector>

使用:

<RadioButton 
                android:id="@+id/rb_obscenity"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:button="@null"
                android:drawablePadding="10dp"
                android:drawableLeft="@drawable/radiobutton_selector_complaint"
                android:textSize="13sp"
                android:text="淫秽色情"/>

效果:

 

posted @ 2016-06-04 13:08  ts-android  阅读(1134)  评论(0编辑  收藏  举报