Android开发之获取单选与复选框的值操作示例

 更新时间:2019年04月05日 12:12:16   作者:水中鱼之1999  
这篇文章主要介绍了Android开发之获取单选与复选框的值操作,结合实例形式分析了Android针对单选按钮、复选框的事件响应、数值获取等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android开发之获取单选与复选框的值操作。分享给大家供大家参考,具体如下:

效果图:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/root"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TableRow>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="性别"/>
    <!--定义一组单选按钮-->
    <RadioGroup
      android:id="@+id/rg"
      android:orientation="horizontal"
      android:layout_gravity="center_horizontal">
      <!--定义两个单选按钮-->
      <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/male"
        android:text="男"
        android:checked="false"/>
      <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/female"
        android:text="女"
        android:checked="false"/>
    </RadioGroup>
  </TableRow>
  <TableRow>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="喜欢的颜色"/>
    <!--定义一个垂直线性布局-->
    <LinearLayout
      android:layout_gravity="center_horizontal"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      <!--定义三个复选框-->
      <CheckBox
        android:id="@+id/color_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="红色"/>
      <CheckBox
        android:id="@+id/color_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="蓝色"/>
      <CheckBox
        android:id="@+id/color_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绿色"/>
    </LinearLayout>
  </TableRow>
  <TextView
    android:id="@+id/show_sex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <Button
    android:id="@+id/show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="显示复选框内容"
    android:textSize="20pt"/>
  <TextView
    android:id="@+id/show_color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</TableLayout>

Java代码:

public class Home extends AppCompatActivity {
  RadioGroup radioGroup01 ;
  TextView textView01 ;
  TextView textView02 ;
  Button button01 ;
  CheckBox checkBox01 ;
  CheckBox checkBox02 ;
  CheckBox checkBox03 ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);//显示manLayout
    //连接组建
    radioGroup01 = (RadioGroup) findViewById(R.id.rg);
    textView01 = (TextView) findViewById(R.id.show_sex);
    textView02 = (TextView) findViewById(R.id.show_color);
    checkBox01 = (CheckBox) findViewById(R.id.color_red);
    checkBox02 = (CheckBox) findViewById(R.id.color_blue);
    checkBox03 = (CheckBox) findViewById(R.id.color_green);
    button01 = (Button) findViewById(R.id.show);
    //添加监听事件
    radioGroup01.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        //根据用户勾选信息改变tip字符串的值
        String tip = checkedId == R.id.male ?
        "您的性别为男" : "您的性别为n女" ;
        //修改show组件文本
        textView01.setText(tip);
      }
    });
    //输出按钮监听事件
    button01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        textView02.setText("喜欢的颜色: \n");
        //筛选复选框信息
        StringBuffer stringBuffer01 = new StringBuffer();
        stringBuffer01.append(textView02.getText().toString());
        if (checkBox01.isChecked()) {
          stringBuffer01.append("红色\n");
        }
        if (checkBox02.isChecked()) {
          stringBuffer01.append("蓝色\n");
        }
        if (checkBox03.isChecked()) {
          stringBuffer01.append("绿色");
        }
        textView02.setText(stringBuffer01.toString());
      }
    });
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android中的Handler与多线程应用实例

    Android中的Handler与多线程应用实例

    这篇文章主要介绍了Android中的Handler与多线程应用实例,本文首先解释一下handler是用来干嘛的,然后通过例子介绍其在多线程中的应用,需要的朋友可以参考下
    2015-03-03
  • Jetpack Compose常用组件详细介绍

    Jetpack Compose常用组件详细介绍

    本篇开始介绍Jetpack Compose 中常用的组件。有一部分之前的文章中也出现过,今天详细说明一下
    2022-10-10
  • android 检查网络连接状态实现步骤

    android 检查网络连接状态实现步骤

    android 如何检查网络连接状态,是android开发中一个常见的问题,本文将介绍如何实现,需要的朋友可以参考下
    2012-12-12
  • Android里实现退出主程序的提示代码

    Android里实现退出主程序的提示代码

    当用户选择"确定",就退出当前的对话框。其中,有个很重要的函数,Activity.finish(),通过调用这个函数,退出当前运行的整个Android程序
    2013-06-06
  • 利用Jetpack Compose绘制可爱的天气动画

    利用Jetpack Compose绘制可爱的天气动画

    Jetpack Compose是用于构建原生Android UI的现代工具包。Jetpack Compose使用更少的代码,强大的工具和直观的Kotlin API,简化并加速了Android上的UI开发。本文将利用Jetpack Compose绘制可爱的天气动画,感兴趣的可以了解一下
    2022-01-01
  • Android滑动冲突的解决技巧

    Android滑动冲突的解决技巧

    Android滑动冲突是Android开发中常见的问题,在一个界面中,可能存在多个View可以响应滑动事件,如果这些View滑动方向一致,则会导致滑动冲突,本文将从原理、使用与优化三个方面,详细介绍Android滑动冲突的解决方式,需要的朋友可以参考下
    2024-01-01
  • Android退出应用最优雅的方式(改进版)

    Android退出应用最优雅的方式(改进版)

    这篇文章主要介绍了Android退出应用最优雅的方式,改进版,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android使用ftp方式实现文件上传和下载功能

    Android使用ftp方式实现文件上传和下载功能

    这篇文章主要介绍了Android使用ftp方式实现文件上传和下载功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • 深入Android Browser配置管理的详解

    深入Android Browser配置管理的详解

    本篇文章是对Android Browser的配置管理进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Android中EditText setText方法的踩坑实战

    Android中EditText setText方法的踩坑实战

    这篇文章主要给大家分享了一些关于Android中EditText setText方法的踩坑记录,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07

最新评论