单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
来源:互联网 发布:ip网络广播草坪音箱 编辑:程序博客网 时间:2024/05/22 15:44
第一步:在Main_xml文件中代码如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <TableRow> <TextView android:text="性别:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16px"/> <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="喜欢的颜色" android:textSize="16px"/> <LinearLayout android:layout_gravity="center_horizontal" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="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_width="wrap_content" android:layout_height="wrap_content"/></TableLayout>
第二步,在MainActivity中的代码如下:
package com.example.yangjian.checkbuttontest;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.widget.RadioGroup;import android.widget.TextView;public class MainActivity extends ActionBarActivity { RadioGroup rg; TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg = (RadioGroup) findViewById(R.id.rg); show = (TextView) findViewById(R.id.show); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { String tip = checkedId == R.id.male ? "您的性别是男人":"您的性别是女人"; show.setText(tip); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
0 0
- 单选按钮(RadioButton)与 复选框(CheckBox)的功能与 用法
- 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
- 单选框(RadioButton)和复选框(CheckBox)的功能与用法
- android单选按钮(RadioButton)、复选框(CheckBox)
- android基本 UI(三)—单选RadioButton与复选按钮CheckBox
- RadioButton(单选按钮)+Checkbox(复选框)
- 【8】CheckBox(复选框)和RadioButton(单选按钮)
- Android-单选按钮RadioButton和复选框checkbox
- Android练习-----单选按钮(RadioButton) && 复选框(CheckBox)
- [Android]笔记10-单选按钮和复选框的功能与用法
- Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习
- UI控件之RadioButton(单选按钮)&Checkbox(复选按钮)
- 第12章:单选扭(RadioButton)和复选框(CheckBox)的功能和用法
- 利用单选钮、复选框获取用户信息(radiobutton checkbox的功能和用法)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- [LeetCode] Insert Interval 插入区间
- MAC安装JDK1.8
- generate parentthese
- hdu5339-Untitled // BestCoder Round #49 ($) 1001 (搜索)
- hdu 5311 Hidden String
- 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
- Windows环境下main()函数传入参数
- 图像锐化算法 C++ 实现
- Thrift 基础教程(一)安装篇
- Shell脚本中$的用法
- mysq 锁机制详解
- java各种读取properties文件方法
- hadoop问题之java.net.NoRouteToHostException: 没有到主机的路由
- hdu 5312 Sequence