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程序设计有所帮助。

(0)

相关推荐

  • Android文字基线Baseline算法的使用讲解

    引言 Baseline是文字绘制时所参照的基准线,只有先确定了Baseline的位置,我们才能准确的将文字绘制在我们想要的位置上.Baseline的概念在我们使用TextView等系统控件直接设置文字内容时是用不到的,但是如果我们想要在Canvas画布上面绘制文字时,Baseline的概念就必不可少了. 我们先了解一下Android中Canvas画布绘制文字的方法,如下图: 参数示意: text,文字内容 x,文字从画布上开始绘制的x坐标(Canvas是一个原点在左上角的平面坐标系) y,Bas

  • Android开发中TextView各种常见使用方法小结

    本文实例讲述了Android开发中TextView各种常见使用方法.分享给大家供大家参考,具体如下: 效果图: XML布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/root" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:

  • Android权限如何禁止以及友好提示用户开通必要权限详解

    Android权限 Android安全架构规定:默认情况下,任何应用都没有权限执行对其他应用.操作系统或用户有不利影响的任何操作.这包括读写用户的私有数据(联系人,短信,相册,位置).读写其他应用的文件.执行网络访问.使设备保持唤醒状态等等. 如果是一些正常的权限(非高危权限),比如网络访问等在应用清单文件(AndroidManifest.xml)中配置,系统会自动授予, 但是如果有一些高危权限,位置,文件存储,短信等这个时候系统会要求用户授予权限,Android 发出权限请求的方式取决于系统版

  • Android中Retrofit的简要介绍

    Retrofit A type-safe HTTP client for Android and Java 适用于Java和Android的安全的HTTP客户端 Retrofit是一个可用于Android和Java的网络库,使用它可以简化我们的网络操作,提高效率和正确率.它将请求过程和底层代码封装起来只暴露我们业务中的请求和返回数据模型. public interface GitHubService { @GET("users/{user}/repos") Call<List&l

  • 史上最全Android build.gradle配置详解(小结)

    Android Studio是采用gradle来构建项目的,gradle是基于groovy语言的,如果只是用它构建普通Android项目的话,是可以不去学groovy的.当我们创建一个Android项目时会包含两个Android build.gradle配置详解文件,如下图: 一.Project的build.gradle文件: 对应的build.gradle代码如下: // Top-level build file where you can add configuration options

  • Android开发实现的圆角按钮、文字阴影按钮效果示例

    本文实例讲述了Android开发实现的圆角按钮.文字阴影按钮效果.分享给大家供大家参考,具体如下: 效果图: 如果要实现圆角图片,并变色须在drawable中配置背景文件如下: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item andro

  • android分享纯图片到QQ空间实现方式

    最新开发新项目的时候,要做分享项目,要求分享有微信,微信朋友圈,QQ,QQ空间,新浪微博这五个,所分享内容包括,分享纯图片,纯文字,图文类型等,要求分享出去的内容不能带有当前app的logo,而无论使用微信分享sdk,还是qq分享sdk,图文类型的分享都会带有当前app的logo和名称,所以笔者最终只能使用android原生实现分享功能了. 一.分享微信,分享微信单独分享一张图片时,可以使用原生分享,也可以使用微信分享sdk,sdk实现方式,笔者不再多述,网上太多,可以看官方说明: (1)  微

  • Android开发实现Switch控件修改样式功能示例【附源码下载】

    本文实例讲述了Android开发实现Switch控件修改样式功能.分享给大家供大家参考,具体如下: Android中自带的Switch控件在很多时候总觉得和整体系统风格不符,很多时候,自定义Switch是一种方法. 但其实不用这么麻烦,安卓自带的Switch通过修改一些属性,也可以达到和自定义Switch差不多的一个效果. 个人感觉,Switch的属性设置和其他控件还是有挺大区别的.因此,写下此文,方便有需要的同学参考. 先上效果图: 以上便是修改后效果 与 原生Switch的效果对比.代码在文

  • Android开发实现的计时器功能示例

    本文实例讲述了Android开发实现的计时器功能.分享给大家供大家参考,具体如下: 效果图: 布局: 三个按钮 加上一个Chronometer <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.a

  • Android开发之ListView的简单用法及定制ListView界面操作示例

    本文实例讲述了Android开发之ListView的简单用法及定制ListView界面操作.分享给大家供大家参考,具体如下: 效果: 如何从获得listview上item的内容 详见:https://www.jb51.net/article/158000.htm 中遇到的问题部分. 布局实现: 有个listview显示 一个edit和button发送 <?xml version="1.0" encoding="utf-8"?> <RelativeL

随机推荐