[Android]笔记10-单选按钮和复选框的功能与用法
来源:互联网 发布:ubuntu安装qq2017 编辑:程序博客网 时间:2024/05/17 22:26
单选钮(RadioButton)、复选框(CheckBox)、状态开关按钮(ToggleButton)和开关(Switch)是用户界面中最普遍的UI组件,他们都继承了button类,因此都可直接使用button支持的各种属性和方法。
RadioButton、Checkbox与普通按钮不同的是,他们多了一个可选中的功能,因此RadioButton、Checkbox都额外指定一个checked属性,该属性用于指定RadioButton、Checkbox初始时是否被选中
XML:
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.dezai.checkbuttontest.MainActivity"><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="true"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/female" android:text="女" /> </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_height="wrap_content" android:layout_width="wrap_content"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="红色" android:checked="true"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="蓝色"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绿色"/></LinearLayout></TableRow> <TextView android:id="@+id/show" android:layout_height="wrap_content" android:layout_width="wrap_content"/></TableLayout>
java
package com.dezai.checkbuttontest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.RadioGroup;import android.widget.TextView;public class MainActivity extends AppCompatActivity { RadioGroup rg; TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取界面上rg/show两个组件 rg=(RadioGroup)findViewById(R.id.rg); show=(TextView) findViewById(R.id.show); //为RadionGroup 组件的OnCheckedChanged事件绑定事件监听器 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group,int checkedId){ // String tip=checkedId==R.id.male?"你的性别是男人":"你的性别是女人"; show.setText(tip); } }); }}
来自:疯狂讲义
阅读全文
0 0
- [Android]笔记10-单选按钮和复选框的功能与用法
- 单选按钮(RadioButton)与 复选框(CheckBox)的功能与 用法
- 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
- Android学习笔记17---单选按钮&复选框
- Android 单选按钮和复选框事例
- Android-单选按钮RadioButton和复选框checkbox
- Android中的 单选按钮和复选框
- 复选框与单选按钮
- 2复选框和单选按钮
- 单选按钮和复选框整合
- 单选按钮和复选框
- 单选按钮和复选框
- 单选按钮和复选框
- 单选按钮和复选框的数据绑定
- 2.2 复选框和单选按钮的使用实例
- 6.单选按钮和复选框的应用实例
- 一看就会Android之单选按钮和复选框的事件监听
- 单选按钮与复选按钮的相互转换
- Qt5汉字乱码
- RHEL6.4使用yum提示需要注册
- Qt4.8.6 VS2010 QWT6.1.2
- Linux API函数大全
- Linux安装QT4.8.6和Qt Creator
- [Android]笔记10-单选按钮和复选框的功能与用法
- CPA备考经验贴汇
- Linux系统下测试磁盘读写速度技巧
- 关于Linux下Qt执行shell命令
- tar.gz tar.bz2文件解压命令
- Linux安装gcc4.8.4
- Linux记录
- Qt5.6.2安装过程中出现的问题
- Linux Qt安装QWT6.1.2