Android学习笔记——CheckBox
2015-06-15 16:44
387 查看
该工程的功能实现在一个activity中显示一个单选框和一个多选框
以下代码是MainActivity.java文件中的代码
以下代码是activity_main.xml文件中的代码
以下代码是string.xml文件中的代码
以下代码是MainActivity.java文件中的代码
package com.example.checkbox; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends Activity { //对控件对象进行声明 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 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //通过控件的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); //设置监听器 gendergroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(femaleButton.getId() == checkedId){ System.out.println("female"); Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show(); } else if(maleButton.getId() == checkedId) { System.out.println("male"); Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show(); } } }); //为多选按钮添加监听器 swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("swim is checked"); } else { System.out.println("swim is unchecked"); } } }); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("run is checked"); } else { System.out.println("run is unchecked"); } } }); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("read is checked"); } else { System.out.println("read is unchecked"); } } }); } }
以下代码是activity_main.xml文件中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <RadioGroup android: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> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /> <CheckBox android: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="app_name">CheckBox</string> <string name="hello_world">Hello world!</string> <string name="female">女</string> <string name="male">男</string> <string name="swim">游泳</string> <string name="run">跑步</string> <string name="read">读书</string> </resources>
相关文章推荐
- Android进程管理
- Android的Menu菜单详解(一)
- android MVP使用
- Android开发之MediaPlayerService服务详解(一)
- Android编程之GridView(九宫格)
- Android的线程间的通信问题
- android apk 防止反编译技术第五篇-完整性校验
- android apk 防止反编译技术第三篇-加密
- Android studio 设置自动导包
- Android View 事件分发机制 源码解析
- Android 开发规范
- Android-自定义图像资源的使用(1)
- android apk 防止反编译技术第二篇-运行时修改Dalvik指令
- Android 回调机制在监听用户界面操作中的体现
- Android json数据解析
- Android JSON数据解析(GSON方式)
- android lcd帧率
- 应用程序的插件化
- Android学习笔记——Button
- android学习笔记(二)布局