首页 > 其他分享 >3.11号今日总结

3.11号今日总结

时间:2023-03-11 21:00:40浏览次数:32  
标签:总结 Toast CheckBox 3.11 RadioButton 单选 按钮 今日 cb

1.基本用法与事件处理:


1)RadioButton(单选按钮)

如题单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现 单选功能!先熟悉下如何使用RadioButton,一个简单的性别选择的例子: 另外我们可以为外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平~

效果图:

PS:笔者的手机是Android 5.0.1的,这里的RadioButton相比起旧版本的RadioButton,稍微好看一点~

布局代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别"
        android:textSize="23dp"
        />

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

        <RadioButton
            android:id="@+id/btnMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"/>

        <RadioButton
            android:id="@+id/btnWoman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>

    <Button
        android:id="@+id/btnpost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>

</LinearLayout>

获得选中的值:

这里有两种方法,

第一种是为RadioButton设置一个事件监听器setOnCheckChangeListener

例子代码如下:

RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
        //第一种获得单选按钮值的方法  
        //为radioGroup设置一个监听器:setOnCheckedChanged()  
        radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radbtn = (RadioButton) findViewById(checkedId);
                Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
            }
        });

运行效果图: 

PS:另外有一点要切记,要为每个RadioButton添加一个id,不然单选功能会生效!!!

第二种方法是通过单击其他按钮获取选中单选按钮的值,当然我们也可以直接获取,这个看需求~

例子代码如下:

Button btnchange = (Button) findViewById(R.id.btnpost);
        RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
        //为radioGroup设置一个监听器:setOnCheckedChanged()  
        btnchange.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < radgroup.getChildCount(); i++) {
                    RadioButton rd = (RadioButton) radgroup.getChildAt(i);
                    if (rd.isChecked()) {
                        Toast.makeText(getApplicationContext(), "点击提交按钮,获取你选择的是:" + rd.getText(), Toast.LENGTH_LONG).show();
                        break;
                    }
                }
            }
        });

运行效果图:

代码解析: 这里我们为提交按钮设置了一个setOnClickListener事件监听器,每次点击的话遍历一次RadioGroup判断哪个按钮被选中我们可以通过下述方法获得RadioButton的相关信息!

  • getChildCount( )获得按钮组中的单选按钮的数目;
  • getChinldAt(i):根据索引值获取我们的单选按钮
  • isChecked( ):判断按钮是否选中

2)CheckBox(复选框)

如题复选框,即可以同时选中多个选项,至于获得选中的值,同样有两种方式: 1.为每个CheckBox添加事件:setOnCheckedChangeListener 2.弄一个按钮,在点击后,对每个checkbox进行判断:isChecked();

运行效果图:

实现代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{

    private CheckBox cb_one;
    private CheckBox cb_two;
    private CheckBox cb_three;
    private Button btn_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cb_one = (CheckBox) findViewById(R.id.cb_one);
        cb_two = (CheckBox) findViewById(R.id.cb_two);
        cb_three = (CheckBox) findViewById(R.id.cb_three);
        btn_send = (Button) findViewById(R.id.btn_send);

        cb_one.setOnCheckedChangeListener(this);
        cb_two.setOnCheckedChangeListener(this);
        cb_three.setOnCheckedChangeListener(this);
        btn_send.setOnClickListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
       if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        String choose = "";
        if(cb_one.isChecked())choose += cb_one.getText().toString() + "";
        if(cb_two.isChecked())choose += cb_two.getText().toString() + "";
        if(cb_three.isChecked())choose += cb_three.getText().toString() + "";
        Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();
    }
}

标签:总结,Toast,CheckBox,3.11,RadioButton,单选,按钮,今日,cb
From: https://www.cnblogs.com/psh888/p/17206919.html

相关文章

  • 2023/3/8 && 2023/3/11 模拟总结
    开摆,嘿嘿,开摆!2023/3/8IOI赛制/6道题/部分文件io集训期间的第一次模拟,看到是IOI赛制感觉好拿分,于是就挺放松的。开考之后看到T1,胡了一个二维前缀和暴力做法,爽拿3......
  • 今日份乐子2
    ......
  • 第二周总结
    这一周,我们上课看了梦想改造家,了解了架构师的作用,了解了软件架构根据要解决的问题,对目标系统的边界进行界定。并对目标系统按某个原则的进行切分。切分的原则,要便于不同......
  • 第三周总结
    大数据课堂测试要求用spark对数据进行清洗处理,可视化展示,完成结果截图如下1、 数据采集及数据预处理   3. 数据统计:生成Hive用户评论数据:(1)在Hive创建一张表,用于......
  • 2023/3/10每日总结
    登录界面xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent......
  • test20230304考试总结(2023春 · 字符串)
    前言赛时得分明细:ABCDTotalRank1001000702702C题如此说道:字符串没有学好的报应!!A.P4391[BOI2009]RadioTransmission无线传输题面给定一个字......
  • 机器学习日志 踩坑总结
    陆续更新中.飞桨AIStudio平台挺好的,要注意的是这个是linux系统,并且不知道为啥这里面的读入test文件是随机的?写了shuffle=False也没用。。。.很难注意的一个点,网络要注......
  • Java常见语法机制总结
    volatile机制CPU与三级缓存:为了解决CPU按照摩尔定律提升的计算能力和内存缓慢发展的不平衡,三级缓存以其比内存更加强悍的读写能力,在CPU和内存中间充当了一层缓存,缓解了这......
  • 2023.03.11.函数重载,引用等
    程序生成的过程:1.预处理:头文件的展开宏的替换预处理指令解析去掉注释2.编译:预处理后文件生成汇编文件.asm(汇编代码)词法解析,语法解析语义分析优化3.汇编:汇编文件进一......
  • odoo ORM API学习总结兼orm学习教程
    环境odoo-14.0.post20221212.tarORMAPI学习总结/学习教程模型(Model)Model字段被定义为model自身的属性fromodooimportmodels,fieldsclassAModel(models.Model......