Android基础教程(六)之----多选项CheckBox的综合应用
2009-11-11 22:29
411 查看
大家好,我们这一节将讲多选项CheckBox
的综合应用,我们的程序主要构造两个CheckBox
的对象,以及一个TextView对象,并通过setOnCheckedChangeLisener
实现onCheckedChanged
()方法来更新TextView
文字.
首先我们看一下效果图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091111/cb1.jpg)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091111/cb2.jpg)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091111/cb3.jpg)
下面是主程序的代码:
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, CheckboxDemo!</string>
<string name="app_name">CheckboxDemo</string>
<string name="hobby">你的爱好是:</string>
<string name="basketball">篮球</string>
<string name="football">足球</string>
</resources>
主程序界面代码main.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"
>
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hobby"
/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/basketball"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/football"
/>
</LinearLayout>
最后是程序的核心代码CheckBoxDemo:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class CheckboxDemo extends Activity {
private TextView tv;
private CheckBox cb1;
private CheckBox cb2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.textview1);
cb1 = (CheckBox)findViewById(R.id.checkbox1);
cb2 = (CheckBox)findViewById(R.id.checkbox2);
cb1.setOnCheckedChangeListener(cbListener);
cb2.setOnCheckedChangeListener(cbListener);
}
private CheckBox.OnCheckedChangeListener cbListener =
new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
String stv = getString(R.string.hobby);
String scb1 = getString(R.string.basketball);
String scb2 = getString(R.string.football);
//判断一共有四种情况
if(cb1.isChecked()== true && cb2.isChecked()== true)
{
tv.setText(stv + scb1 + "," + scb2);
}
else if(cb1.isChecked()== true && cb2.isChecked()== false)
{
tv.setText(stv+scb1);
}
else if(cb1.isChecked() == false && cb2.isChecked() == true)
{
tv.setText(stv+scb2);
}
else{
tv.setText(stv);
}
}
};
}
这一节就至此为止,由于时间的原因,我就不加注释了,有什么不能理解的地方,请大家留言,我会详细解答.
的综合应用,我们的程序主要构造两个CheckBox
的对象,以及一个TextView对象,并通过setOnCheckedChangeLisener
实现onCheckedChanged
()方法来更新TextView
文字.
首先我们看一下效果图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091111/cb1.jpg)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091111/cb2.jpg)
![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091111/cb3.jpg)
下面是主程序的代码:
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, CheckboxDemo!</string>
<string name="app_name">CheckboxDemo</string>
<string name="hobby">你的爱好是:</string>
<string name="basketball">篮球</string>
<string name="football">足球</string>
</resources>
主程序界面代码main.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"
>
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hobby"
/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/basketball"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/football"
/>
</LinearLayout>
最后是程序的核心代码CheckBoxDemo:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class CheckboxDemo extends Activity {
private TextView tv;
private CheckBox cb1;
private CheckBox cb2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.textview1);
cb1 = (CheckBox)findViewById(R.id.checkbox1);
cb2 = (CheckBox)findViewById(R.id.checkbox2);
cb1.setOnCheckedChangeListener(cbListener);
cb2.setOnCheckedChangeListener(cbListener);
}
private CheckBox.OnCheckedChangeListener cbListener =
new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
String stv = getString(R.string.hobby);
String scb1 = getString(R.string.basketball);
String scb2 = getString(R.string.football);
//判断一共有四种情况
if(cb1.isChecked()== true && cb2.isChecked()== true)
{
tv.setText(stv + scb1 + "," + scb2);
}
else if(cb1.isChecked()== true && cb2.isChecked()== false)
{
tv.setText(stv+scb1);
}
else if(cb1.isChecked() == false && cb2.isChecked() == true)
{
tv.setText(stv+scb2);
}
else{
tv.setText(stv);
}
}
};
}
这一节就至此为止,由于时间的原因,我就不加注释了,有什么不能理解的地方,请大家留言,我会详细解答.
相关文章推荐
- Android基础教程(六)之----多选项CheckBox的综合应用
- Android基础教程(六)之----多选项CheckBox的综合应用
- Android基础教程(七)之----单选项框RadioGroup的综合应用
- Android基础教程(5)之----单选项框RadioGroup的综合应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(七)之----单选项框RadioGroup的综合应用
- Android基础教程(七)之----单选项框RadioGroup的综合应用
- Android基础教程(七)之----单选项框RadioGroup的综合应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程之简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(四)之-----取得手机屏幕大小DisplayMetrics的应用
- Android基础教程之-----访Iphone 拖动相片特效Gallery的简单应用.
- Android基础教程(四)之-----取得手机屏幕大小DisplayMetrics的应用
- Android基础教程(九)之自定义下拉菜单模式----Spinner与setDropDownViewResource的应用
- Android基础教程(四)之-----取得手机屏幕大小DisplayMetrics的应用
- Android基础教程(九)之自定义下拉菜单模 be2c 式----Spinner与setDropDownViewResource的应用
- Android基础教程之-----访Iphone 拖动相片特效Gallery的简单应用.