android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
来源:互联网 发布:js将字符串转为数组 编辑:程序博客网 时间:2024/05/16 05:17
android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
本博文主要讲述的是android开发中的单选和多选按钮的使用,具体情况请看实例代码:
MainActivity.java:
package com.example.radiotest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 generGroup = null;
private RadioButton maleButton = null;
private RadioButton femaleButton =null;
private CheckBox swin = null, run = null, jump = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
generGroup = (RadioGroup)findViewById(R.id.mySexGroup);
maleButton = (RadioButton)findViewById(R.id.maleButton);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
swin = (CheckBox)findViewById(R.id.swinBox);
run = (CheckBox)findViewById(R.id.runBox);
jump = (CheckBox)findViewById(R.id.jumpBox);
//设置RadioGroup监听器
generGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == maleButton.getId()){
Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
}
else if(checkedId == femaleButton.getId()){
Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
}
}
});
//设置CheckBox多选的监听器
swin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("swin is checked!");
}
else{
System.out.println("swin is unchecked!");
}
}
});
run.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!");
}
}
});
jump.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("jump is checked!");
}
else{
System.out.println("jump is unchecked!");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 generGroup = null;
private RadioButton maleButton = null;
private RadioButton femaleButton =null;
private CheckBox swin = null, run = null, jump = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
generGroup = (RadioGroup)findViewById(R.id.mySexGroup);
maleButton = (RadioButton)findViewById(R.id.maleButton);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
swin = (CheckBox)findViewById(R.id.swinBox);
run = (CheckBox)findViewById(R.id.runBox);
jump = (CheckBox)findViewById(R.id.jumpBox);
//设置RadioGroup监听器
generGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == maleButton.getId()){
Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
}
else if(checkedId == femaleButton.getId()){
Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
}
}
});
//设置CheckBox多选的监听器
swin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("swin is checked!");
}
else{
System.out.println("swin is unchecked!");
}
}
});
run.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!");
}
}
});
jump.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("jump is checked!");
}
else{
System.out.println("jump is unchecked!");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局文件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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/mySexGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"/>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"/>
</RadioGroup>
<CheckBox
android:id="@+id/swinBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swin"/>
<CheckBox
android:id="@+id/runBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"/>
<CheckBox
android:id="@+id/jumpBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jump"/>
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/mySexGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"/>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"/>
</RadioGroup>
<CheckBox
android:id="@+id/swinBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swin"/>
<CheckBox
android:id="@+id/runBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"/>
<CheckBox
android:id="@+id/jumpBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jump"/>
</LinearLayout>
效果如下:
0 0
- android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
- HTML单选按钮(Radio)样式更改以及多选按钮(Checkbox)样式更改
- html之复选杜框(checkbox)、单选按钮(radio)、下拉菜单(select-option)
- UI控件之RadioButton(单选按钮)&Checkbox(复选按钮)
- android学习总结(16.08.29)单选按钮(RadioButton),多选按钮(CheckBox)和开关按钮(Switch)
- 表单控件(复选框checkbox和单选择按钮radio)
- VC单选按钮控件(Radio Button)用法(转)
- VC单选按钮控件(Radio Button)用法(转)
- VC单选按钮控件(Radio Button)用法(转)
- VC单选按钮控件(Radio Button)用法(转)
- MFC控件-单选按钮(Radio Button)
- VC单选按钮控件(Radio Button)用法(转)
- iOS开发系列之常用自定义控件开发集—自定义单选按钮或者多选按钮控件开发
- Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习
- html <radio>单选按钮控件标签
- MFC学习之:对话框中的单选按钮(Radio)
- Radio Buttons(单选按钮)
- 单选(radio)按钮点击事件
- WeixinJSBridge
- Ubuntu 安装Oracle 11g R2
- 如何一个android工程作为另外一个android工程的lib
- Linux apache tomcat 集群搭建
- 的v私人哥啊任何事
- android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
- struts2学习笔记
- 浅谈软件开发工具的成本核算问题
- SpringSide3中多数据源配置
- Java虚拟机学习(一)
- 系统软件项目成本构成及估算方法
- android:clipChildren妙用
- [LeetCode] Trapping Rain Water
- Bat之For传值