Android初级教程小案例之单选框RadioGroup与复选框CheckBox
来源:互联网 发布:淘宝手机地址转换 编辑:程序博客网 时间:2024/05/17 01:08
Android里面的单选框和html中的其实是一样的效果。这里用到两个控件:CheckBox和RadioGroup。直接上代码:
radio.xml布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextViewandroid:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><RadioGroupandroid:id="@+id/genderGroup" 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><CheckBoxandroid:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /><CheckBoxandroid:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /><CheckBoxandroid:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /></LinearLayout>
String.xml代码:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, Activity07!</string> <string name="app_name">activity07</string> <string name="male">男</string> <string name="female">女</string> <string name="swim">swim</string> <string name="run">run</string> <string name="read">read</string></resources>RadioTest:
package mars.activity07;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 RadioTest extends Activity { /** Called when the activity is first created. *///对控件对象进行声明private RadioGroup genderGroup = 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.radio); //通过控件的ID来得到代表控件的对象 genderGroup = (RadioGroup)findViewById(R.id.genderGroup); 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设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同 genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("famale");Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}else if(maleButton.getId() == checkedId){System.out.println("male");Toast.makeText(RadioTest.this, "male", Toast.LENGTH_SHORT).show();}}}); //为多选按钮添加监听器 swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked)//未选中到选中状态是执行这里:{System.out.println("swim is checked");}else//由选中状态到未选中状态时候执行这里:{System.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");}else{System.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");}else{System.out.println("read is unchecked");}}}); } }
注册文件进行注册:
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".RadioTest" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
执行效果图:
1 1
- Android初级教程小案例之单选框RadioGroup与复选框CheckBox
- RadioGroup与Radio、复选框CheckBox
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 第7章、单选按钮RadioGroup与复选框CheckBox(从零开始学Android)
- 单选按钮RadioGroup与复选框CheckBox
- Android--CheckBox复选框、RadioGroup和RadioButton及其使用
- Android之RadioButton与RadioGroup,CheckBox,Switch,ToggleButton的介绍
- Android之复选框CheckBox使用
- 第6章、单选按钮RadioGroup与复选框CheckBox
- Android开发——单选RadioGroup和复选CheckBox
- Android 复选框CheckBox
- Android初级教程三个Dialog对话框小案例
- 初学安卓路之CheckBox与RadioGroup
- Android之CheckBox,RadioGroup(笔记)
- Android之CheckBox,RadioGroup(笔记)
- Android控件之CheckBox、RadioGroup、RadioButton
- Leetcode_c++:Pascal's Triangle(118)
- JavaScript实现页面的后退前进按钮
- 一个支持cookie的简单web服务器
- 用.Net在AutoCAD中创建包含文字的线型
- FreeRTOS高级篇8---FreeRTOS任务通知分析
- Android初级教程小案例之单选框RadioGroup与复选框CheckBox
- 【Java】letter combinations of phone number
- Java(数据类型,数组表达,Unicode编码)
- leetcode 19. Remove Nth Node From End of List
- 错误:找不到或无法加载主类HelloWorld
- android /system/vold源码分析(4)
- 一个 JavaScript 小时钟
- JavaAPI之AtomicInteger
- Spark之Streaming实时监听Hdfs文件目录