安卓开发学习-按钮控件

java代码

点击查看代码
package com.android.myapp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ButtonActivity extends AppCompatActivity {

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);

        // 获取 CheckBox 控件
        CheckBox ck_btn = findViewById(R.id.ck_btn);
        ck_btn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            // 根据勾选状态设置文本
            if (isChecked) {
                buttonView.setText("勾选");
            } else {
                buttonView.setText("未勾选");
            }
        });

        // 获取 Switch 控件
        @SuppressLint("UseSwitchCompatOrMaterialCode") Switch sc_btn = findViewById(R.id.sc_btn);
        sc_btn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            // 根据开关状态设置文本
            if (isChecked) {
                buttonView.setText("打开");
            } else {
                buttonView.setText("关闭");
            }
        });

        // 获取 RadioGroup 控件和显示结果的 TextView
        RadioGroup rg_btn = findViewById(R.id.rg_btn);
        TextView tv_rg_btn_res = findViewById(R.id.tv_rg_btn_res);

        rg_btn.setOnCheckedChangeListener((group, checkedId) -> {
            // 根据选中的单选按钮设置文本
            if (checkedId == R.id.case_1) {
                tv_rg_btn_res.setText("你选择了选项一");
            } else if (checkedId == R.id.case_2) {
                tv_rg_btn_res.setText("你选择了选项二");
            }
        });
    }
}

xml配置文件

点击查看代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".ButtonActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="勾选按钮">

    </TextView>

    <CheckBox
        android:id="@+id/ck_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </CheckBox>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开关按钮">

    </TextView>

    <Switch
        android:id="@+id/sc_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Switch>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择按钮">

    </TextView>

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

        <RadioButton
            android:id="@+id/case_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="选项一">

        </RadioButton>

        <RadioButton
            android:id="@+id/case_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="选项二">

        </RadioButton>

    </RadioGroup>

    <TextView
        android:id="@+id/tv_rg_btn_res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </TextView>
</LinearLayout>

效果图

image

posted on 2024-03-10 20:02  江城qwe  阅读(23)  评论(0编辑  收藏  举报