常用控件:单选框和复选框

一.思维导图
在这里插入图片描述
二.继承关系图
(1)单选按钮组
在这里插入图片描述
(2)单选按钮
在这里插入图片描述
(3)复选框
在这里插入图片描述
三.实例
(一)、运行效果
在这里插入图片描述
(二)实现步骤
1.创建安卓应用
2.布局资源文件

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="30dp">

    <TextView
        android:id="@+id/tv_set_information"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="30dp"
        android:text="@string/set_information"
        android:textColor="#0000ff"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/edt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_name"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gender"
            android:textColor="#000000"
            android:textSize="16sp" />

        <RadioGroup
            android:id="@+id/rg_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/male" />

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/female" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hobby"
            android:textColor="#000000"
            android:textSize="16sp" />

        <CheckBox
            android:id="@+id/cb_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/music" />

        <CheckBox
            android:id="@+id/cb_read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/read" />

        <CheckBox
            android:id="@+id/cb_food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/food" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doOK"
            android:text="@string/ok" />

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doClear"
            android:text="@string/clear" />

        <Button
            android:id="@+id/btn_exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doExit"
            android:text="@string/exit" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="15sp" />

</LinearLayout>

3.字符串资源文件

<resources>
    <string name="app_name">设置基本信息</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="single">单身</string>
    <string name="married">已婚</string>
    <string name="music">音乐</string>
    <string name="read">阅读</string>
    <string name="food">美食</string>
    <string name="ok">确定</string>
    <string name="clear">清除</string>
    <string name="exit">退出</string>
    <string name="set_information">设置基本信息</string>
    <string name="gender">性别:</string>
    <string name="marriage">婚姻:</string>
    <string name="hobby">爱好:</string>
    <string name="name">姓名:</string>
    <string name="input_name">请输入姓名</string>
</resources>

4,主界面

public class MainActivity extends Activity {
    private EditText editText;
    private TextView textView2;
    private RadioGroup rgSingleChoice;
    private RadioButton rbSingleA;
    private RadioButton rbSingleB;
    private CheckBox cbMultipleA;
    private CheckBox cbMultipleB;
    private CheckBox cbMultipleC;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.edt_name);
        textView2 = findViewById(R.id.tv_result);
        rgSingleChoice = findViewById(R.id.rg_gender);
        rbSingleA = findViewById(R.id.rb_male);
        rbSingleB = findViewById(R.id.rb_female);
        cbMultipleA = findViewById(R.id.cb_music);
        cbMultipleB = findViewById(R.id.cb_read);
        cbMultipleC = findViewById(R.id.cb_food);


    }
    public void Save(View view){
        String s="性别:";
        if(rbSingleA.isChecked()){
            s+="男";
        }else if(rbSingleB.isChecked()){
            s+="女";
        }
        s+="  爱好:";
        if(cbMultipleA.isChecked()){
            s+="音乐";
        }
        if(cbMultipleB.isChecked()){
            s+="阅读";
        }
        if(cbMultipleC.isChecked()){
            s+="美食";
        }
        textView2.setText(s);
    }

    public void doExit(View view){
        finish();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值