安卓RadioButton,RadioGroup,Button按钮的使用

来源:互联网 发布:淘宝上有正品阿迪吗 编辑:程序博客网 时间:2024/05/28 05:14

        在安卓中,单选按钮和复选按钮都继承至普通按钮,因此他们都可以使用普通按钮支持的各种属性和方法。与普通按钮不同的是,单选和复选按钮提供了可选择的功能。本片先介绍单选按钮及单选按钮组的使用。

        这里我会将RadioButton和RadioGroup以及Button和TextView搭配使用。这样便于比较异同,下面直接上代码。

布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/content_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.example.myapplication.MainActivity"    tools:showIn="@layout/app_bar_main">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:gravity="center_vertical|center_horizontal"        >        <TextView            android:id="@+id/textView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="性别:"            />        <RadioGroup            android:id="@+id/radioGroup1"            android:layout_toRightOf="@id/textView1"            android:orientation="horizontal"            android:layout_width="wrap_content"            android:layout_height="wrap_content">            <RadioButton                android:id="@+id/radio1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="男"                android:checked="true"                />            <!-- android:checked="true"默认设置为选中状态 -->            <RadioButton                android:id="@+id/radio2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="女"                />        </RadioGroup>        <Button            android:id="@+id/button1"            android:layout_toRightOf="@id/radioGroup1"            android:text="提交"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout>    <TextView        android:id="@+id/textView2"        android:layout_alignParentBottom="true"        android:layout_width="match_parent"        android:layout_height="100dp" /></RelativeLayout>

效果图:

JAVA代码:

private RadioGroup radioGroup;    private RadioButton radioButton1;    private RadioButton radioButton2;    private Button button;    private TextView textView;


 radioGroup=(RadioGroup)findViewById(R.id.radioGroup1);        radioButton1=(RadioButton)findViewById(R.id.radio1);        radioButton2=(RadioButton)findViewById(R.id.radio2);        button=(Button)findViewById(R.id.button1);        textView=(TextView)findViewById(R.id.textView2);        //在改变单选按钮组的时候获取其值        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton radioButton=(RadioButton)findViewById(checkedId);                textView.append("group"+radioButton.getText());            }        });        //单击其他按钮时获取        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                for (int i=0;i<radioGroup.getChildCount();i++){                    RadioButton radioButton=(RadioButton)radioGroup.getChildAt(i);                    if (radioButton.isChecked()){                        textView.append("Button"+radioButton.getText());                        break;                    }                }            }        });



0 0
原创粉丝点击