栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android学习笔记篇2. 单选按钮、复选按钮

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android学习笔记篇2. 单选按钮、复选按钮

单选按钮

在XML里写一组单选按钮(2个)+ 一个文本(用于提示):




    
        
        
    

    


在Java中编写代码

package com.example.no_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.rdg);
        textView = (TextView) findViewById(R.id.tv);
        
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                // 判断被点击的RadioButton
                if (i == R.id.rbtn) {
                    textView.setText("您的性别是:男");
                } else {
                    textView.setText("您的性别是:女");
                }
            }
        });
    }
}

运行,点击按钮,在底下提示相应信息:

复选按钮

在XML里写一组复选按钮 + 两个提示文本 + 一个显示输出文本




    

    
    

    
    
    


在Java中编写代码,利用TextView提示哪一个复选框被选择:

package com.example.no_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView hobby;
    private String hobbys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        CheckBox shuttlecock = (CheckBox) findViewById(R.id.shuttlecock);
        CheckBox basketball = (CheckBox) findViewById(R.id.basketball);
        CheckBox pingpong = (CheckBox) findViewById(R.id.pingpong);
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpong.setOnCheckedChangeListener(this);
        hobby = (TextView) findViewById(R.id.hobby);
        hobbys = new String();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();
        
        if (b) {
            if (!hobbys.contains(motion)) {
                hobbys = hobbys + motion;
                hobby.setText(hobbys);
            }
        } else {
            if (hobbys.contains(motion)) {
                hobbys = hobbys.replace(motion, "");
                hobby.setText(hobbys);
            }
        }
    }
}

运行,在底下提示对应信息:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号