随笔 - 7  文章 - 0  评论 - 0  阅读 - 521

安卓开发学习-按钮控件

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   江城qwe  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示