Android学习第12课—常用控件(二)
来源:互联网 发布:淘宝贷款不还会怎样 编辑:程序博客网 时间:2024/06/05 05:03
主要内容:
1 RadioGroup和RadioButton的使用方法
这两个控件代表Android中单选按钮的控件,单选按钮必须要创建一个组
2 CheckBox的使用方法
CheckBox是多选按钮
3 Toast的基本用法
Toast控件类似弹出的提示信息
1 RadioGroup和RadioButton的使用方法
这两个控件代表Android中单选按钮的控件,单选按钮必须要创建一个组
2 CheckBox的使用方法
CheckBox是多选按钮
3 Toast的基本用法
Toast控件类似弹出的提示信息
注意:RadioGroup和CheckBox使用的监听器的OnCheckedChangeLiatener不一样—RadioGroup要使用的对象不是CompoundButton的子类,所以不能使用CompoundedButton,所以他只能重新编写一个OnCheckedChangeListener的接口; 而CheckBox是CompoundButton的子类,所以他能直接使用CompoundButton。
布局文件实例:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/radiogroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> </RadioGroup> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="swim" /> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="run" /> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="read" /></LinearLayout>
Activity文件:
package dj.activity;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class Activity13 extends Activity { /** Called when the activity is first created. */private RadioGroup radioGroup = null;private RadioButton femaleButton = null;private RadioButton maleButton = null;private CheckBox swimBox = null;private CheckBox runBox = null;private CheckBox readBox = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); radioGroup = (RadioGroup)findViewById(R.id.radiogroup); femaleButton = (RadioButton)findViewById(R.id.femaleButton); maleButton = (RadioButton)findViewById(R.id.maleButton); swimBox = (CheckBox)findViewById(R.id.swim); runBox = (CheckBox)findViewById(R.id.run); readBox = (CheckBox)findViewById(R.id.read); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("female");Toast.makeText(Activity13.this, "female", Toast.LENGTH_SHORT);}else if(maleButton.getId() == checkedId){System.out.println("male");//Toast.makeText(Activity13.this, "male", Toast.LENGTH_SHORT);}}}); swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked)System.out.println("swim is checked!");elseSystem.out.println("swim is unchecked!");}}); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked)System.out.println("run is checked!");elseSystem.out.println("run is unchecked!");}}); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked)System.out.println("read is checked!");elseSystem.out.println("read is unchecked!");}}); }}
- Android学习第12课—常用控件(二)
- Android学习第13课—常用控件(三)
- Android—常用控件(二)按钮
- Android常用控件(二)
- Android 常用控件(二)
- android常用控件(二)
- android学习笔记(二)—— 常用布局与控件
- Android学习第6课—常见控件(一)
- Android常用控件(二)
- android常用控件(二)
- Android常用控件二
- Android常用控件二
- Android学习第8课—Activity生命周期(二)
- Android学习笔记:<二>03.Android当中的常用控件
- Android常用控件(二)【ProgressBar、ListView】
- Android常用自定义控件(二)
- Android常用控件之ListView(二)
- Android学习第三天:页面布局和常用控件
- 把路由器变成音乐播放器和网络收音机,支持摇控
- DataTable分组求和
- 颜色空间转换YCbCr2RGB/RGB2YCbCr/YCbCr2HSI/HSI2YCbCr/YCbCr2TSL/TSL2YCbCr
- 跨平台的模块时间统计套件(C++编写)
- 几种通讯协议的比较
- Android学习第12课—常用控件(二)
- Rational AppScan 扫描大型网站
- c++ mem开头函数
- deb ipa pxl
- Javascript----文件操作
- 关于IE9卸载的解决方案
- Android Service 的使用(三)----远程Service的调用
- .javascript字符串处理函数
- 20---IO学习笔记(补充)