标签:
12. CheckBox 的使用
1)通过只含有一个CheckBox的实例来学习CheckBox的使用
示例代码
① 创建新工程
② 在string.xml 中添加字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Ex_Ctrl_4</string>
<string name="advice">请勾选我同意</string>
<string name="accept">您已接受同意!!</string>
<string name="Notaccept">您未同意!!</string>
<string name="allOK">全部许可</string>
</resources>
③ 修改main.xml 布局,添加UI 元素
……
<CheckBox
android:layout_height="wrap_content"
android:layout_width="120px"
android:layout_marginTop="100px"
android:layout_marginLeft="90px"
android:id="@+id/CheckBox_Accept" >
</CheckBox>
……
④ 修改mainActivity.java 文件
package zyf.Ex_Ctrl_4;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class Ex_Ctrl_4 extends Activity {
// Called when the activity is first created.
private TextView showAdvice,yourChoiceshow;
private CheckBox iAccept;
private Button ok;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//findViewById()从资源ID获取资源对象
showAdvice=(TextView)findViewById(R.id.TextView_Guide);
yourChoiceshow=(TextView)findViewById(R.id.TextView_youChoiceShow );
iAccept=(CheckBox)findViewById(R.id.CheckBox_Accept);
ok=(Button)findViewById(R.id.Button_OK);
//获取XML中字符串
CharSequence titleString=getString(R.string.allOK);
//设置复选框标题
iAccept.setHint(titleString);
//设置复选框标题字体颜色
iAccept.setHintTextColor(Color.RED);
//将CheckBox设置成未选中
iAccept.setChecked(false);
//将Button设置成不可用
ok.setEnabled(false);
iAccept.setOnClickListener(new CheckBox.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(iAccept.isChecked()){
ok.setEnabled(true);
yourChoiceshow.setText(R.string.accept);
}else{
ok.setEnabled(false);
yourChoiceshow.setText(R.string.Notaccept);
}
}
});
ok.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(iAccept.isChecked()){
showAdvice.setText(R.string.accept);
}
}
});
}
}
⑤ 结果
关键点
1. CheckBox 的 isChecked() 方法
public abstract boolean isChecked ()
返回值:The current checked state of the view 。当前CheckBox的状态 True:选中 False:未选
2. CheckBox 的 setChecked() 方法
public abstract void setChecked (boolean checked)
作用:修改 CheckBox 的状态
参数:The new checked state .
3. Button 的 setEnabled() 方法
public void setEnabled (boolean enabled)
作用:设置 view 的使能状态
参数:view 的新使能状态 True 使能 False 禁用
2)含多个CheckBox 的实例
示例代码
① 新建工程
② 在string.xml 中添加字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Ex_Ctrl_5</string>
<string name="shoopingList_TextView_text" >网上购物商品清单</string>
<string name="yourChocieWoods_text" >您选择购买的商品:</string>
<string name="woods_Text_MP4">纽曼MP4</string>
<string name="woods_Text_musicCD">Beyond乐队CD</string>
<string name="woods_Text_book">Android程序员指南</string>
</resources>
③ 修改main.xml 布局,添加UI 元素
……
<CheckBox
android:layout_height="wrap_content"
android:id="@+id/CheckBox_MP4"
android:text="@string/woods_Text_MP4"
android:layout_width="180px">
</CheckBox>
<CheckBox
android:layout_height="wrap_content"
android:text="@string/woods_Text_musicCD"
android:id="@+id/CheckBox_musicCD"
android:layout_width="180px">
</CheckBox>
<CheckBox
android:layout_height="wrap_content"
android:text="@string/woods_Text_book"
android:id="@+id/CheckBox_book"
android:layout_width="180px">
</CheckBox>
……
④ 修改mainActivity.java,添加逻辑判断
package zyf.Ex_Ctrl_5;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class Ex_Ctrl_5 extends Activity {
//Called when the activity is first created.
private TextView showyourChoice_TextView;
private CheckBox mp4_CheckBox, musicCD_CheckBox, book_CheckBox;
private String showinfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// findViewById()从XML中获取资源对象
showyourChoice_TextView = (TextView)
findViewById(R.id.TextView_yourWoodsList );
mp4_CheckBox = (CheckBox) findViewById(R.id.CheckBox_MP4);
musicCD_CheckBox = (CheckBox) findViewById(R.id.CheckBox_musicCD);
book_CheckBox = (CheckBox) findViewById(R.id.CheckBox_book );
//为三个CheckBox设置选择状态改变事件监听器
mp4_CheckBox.setOnCheckedChangeListener(CheckedChangeListener);
musicCD_CheckBox.setOnCheckedChangeListener(CheckedChangeListener);
book_CheckBox.setOnCheckedChangeListener(CheckedChangeListener);
//从XML中获取显示信息String
showinfo = getString(R.string.yourChocieWoods_text );
}
// 内部接口实现
private OnCheckedChangeListener CheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// TODO Auto-generated method stub
// 处理选中状态改变事件,动态显示选择结果
if (mp4_CheckBox.isChecked()) {
showinfo = getString(R.string.woods_Text_MP4) + "\n";
showString();
} else if (musicCD_CheckBox.isChecked()) {
showinfo = getString(R.string.woods_Text_musicCD) + "\n";
showString();
} else if (book_CheckBox.isChecked()) {
showinfo = getString(R.string.woods_Text_book ) + "\n";
showString();
} if (
mp4_CheckBox.isChecked() && musicCD_CheckBox.isChecked()) {
showinfo = getString(R.string.woods_Text_MP4) + "\n"
+ getString(R.string.woods_Text_musicCD ) + "\n";
showString();
} else if (mp4_CheckBox.isChecked() && book_CheckBox.isChecked()) {
showinfo = getString(R.string.woods_Text_MP4) + "\n"
+ getString(R.string.woods_Text_book ) + "\n";
showString();
} else if (musicCD_CheckBox.isChecked() && book_CheckBox.isChecked()) {
showinfo = getString(R.string.woods_Text_musicCD) + "\n"
+ getString(R.string.woods_Text_book ) + "\n";
showString();
} if (mp4_CheckBox.isChecked() && musicCD_CheckBox.isChecked() && book_CheckBox.isChecked()) {
showinfo = getString(R.string.woods_Text_MP4) + "\n"
+ getString(R.string.woods_Text_musicCD ) + "\n"
+ getString(R.string.woods_Text_book ) + "\n";
showString();
} if (mp4_CheckBox.isChecked() == false && musicCD_CheckBox.isChecked() == false
&& book_CheckBox.isChecked() == false) {
showyourChoice_TextView.setText("");
}
}
public void showString() {
showyourChoice_TextView.setText(showinfo);
}
};
}
⑤ 结果
关键点
1. setOnCheckedChangeListener
public void setOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)
作用:
Register a callback to be invoked when the checked state of this button changes
参数:
listener :the callback to call on checked state change .
13. RadioButton 单选
1)RadioGroup 组与 onCheckedChanged 事件
示例代码
① 新建工程
② string.xml 中添加字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Ex_Ctrl_6</string>
<string name="iam_Boy">帅哥</string>
<string name="iamGirl">美女</string>
<string name="ask">请问你是??</string>
</resources>
③ 修改mian.xml 布局,添加UI 元素
……
<TextView …… >
</TextView>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/RadioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioButton_Boy"
android:text="@string/iam_Boy"></RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/iamGirl"
android:id="@+id/RadioButton_Gril"></RadioButton>
</RadioGroup>
……
④ 修改mainActivity.java 文件
package zyf.Ex_Ctrl_6;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Ex_Ctrl_6 extends Activity {
// Called when the activity is first created.
private TextView answer_TextView;
private RadioButton boy_RadioButton,girl_RadioButton;
private RadioGroup radioGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// findViewById()从XML中获取资源对象
answer_TextView=(TextView)findViewById(R.id.TextView_Ask_And_Show);
radioGroup=(RadioGroup)findViewById(R.id.RadioGroup);
boy_RadioButton=(RadioButton)findViewById(R.id.RadioButton_Boy );
girl_RadioButton=(RadioButton)findViewById(R.id.RadioButton_Gril);
//给单RadioGroup添加状态改变监听器
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(boy_RadioButton.isChecked()){
answer_TextView.setText(R.string.iam_Boy);
}else{
answer_TextView.setText(R.string.iamGirl);
}
}
});
}
}
⑤ 结果
关键点
1. RadioGroup
参看上例中的 xml 布局文件 和 使用方法.
2. onCheckedChanged 事件
public abstract void onCheckedChanged (CompoundButton buttonView, boolean isChecked)
抽象方法,需要工程师自己实现。当 check状态 改变时,会被调用。
参数:
buttonView :The compound button view whose state has changed.
isChecked :The new checked state of buttonView
标签:
原文地址:http://www.cnblogs.com/wj033/p/4569699.html