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

Android案例(单选复选框)

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

Android案例(单选复选框)

单选框

1、实现效果

2、布局



    
        
        
        
        
    
    


3、java代码

package com.example.check_box_application;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=findViewById(R.id.rg);
        textView=findViewById(R.id.xianshi);
        radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            if(i==R.id.one){
                textView.setText("你选择了幻想现纯");
            }else if(i==R.id.two){
                textView.setText("你选择了架空历史");
            }else{
                textView.setText("你选择了无cp");
            }
        });
    }
}
复选框

1、实现效果

2、布局



    
    
    
    
    
    
    
    


3、java代码

package com.example.check_box_application;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class Fu_xuan_Activity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private String los;
    private TextView lo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fu_xuan_layout);
        TextView tv=findViewById(R.id.tv);
        CheckBox one=findViewById(R.id.one);
        CheckBox two=findViewById(R.id.two);
        CheckBox three=findViewById(R.id.three);
        CheckBox four=findViewById(R.id.four);
        CheckBox five=findViewById(R.id.five);
        CheckBox six=findViewById(R.id.six);


        one.setOnCheckedChangeListener(this);
        two.setOnCheckedChangeListener(this);
        three.setOnCheckedChangeListener(this);
        four.setOnCheckedChangeListener(this);
        five.setOnCheckedChangeListener(this);
        six.setOnCheckedChangeListener(this);

        lo=findViewById(R.id.tv);
        los=new String();
    }

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

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

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

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