android应用开发揭秘examples_04-08笔记(CheckBox的使用)
2013-02-26 15:59
786 查看
与前一个例子的RadioBox比起来,这个CheckBox没有什么要多讲的,每一个checkbox 都是独立的处理过程
package com.yarin.android.Examples_04_08;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class Activity01 extends Activity
{
//用来显示题目
TextView m_TextView1;
//“提交按钮”
Button m_Button1;
//4个多选项
CheckBox m_CheckBox1;
CheckBox m_CheckBox2;
CheckBox m_CheckBox3;
CheckBox m_CheckBox4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_TextView1 = (TextView) findViewById(R.id.TextView1);
m_Button1 = (Button) findViewById(R.id.button1);
/* 取得每个CheckBox对象 */
m_CheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
m_CheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
m_CheckBox3 = (CheckBox) findViewById(R.id.CheckBox3);
m_CheckBox4 = (CheckBox) findViewById(R.id.CheckBox4);
m_CheckBox1.setChecked(true);
m_CheckBox4.setChecked(true);
//对每个选项设置事件监听
m_CheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(m_CheckBox1.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox1.getText());
}
}
});
////////////////////
m_CheckBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(m_CheckBox2.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox2.getText());
}
}
});
/////////////////
m_CheckBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(m_CheckBox3.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox3.getText());
}
}
});
////////////////
m_CheckBox4.setOnCheckedChangeListener(new MyClass());//对第4个checkbox变换一下写法练习一下
//对按钮设置事件监听
m_Button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
int num = 0;
if(m_CheckBox1.isChecked())
{
num++;
}
if(m_CheckBox2.isChecked())
{
num++;
}
if(m_CheckBox3.isChecked())
{
num++;
}
if(m_CheckBox4.isChecked())
{
num++;
}
DisplayToast("谢谢参与!你一共选择了"+num+"项!");
}
});
}
class MyClass implements CheckBox.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(m_CheckBox4.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox4.getText());
}
else
{
DisplayToast("我晕,你怎么不选这项了! "+m_CheckBox4.getText());
}
}
}
/* 显示Toast */
public void DisplayToast(String str)
{
Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
//设置toast显示的位置
toast.setGravity(Gravity.TOP, 0, 220);
//显示该Toast
toast.show();
}
}
package com.yarin.android.Examples_04_08;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class Activity01 extends Activity
{
//用来显示题目
TextView m_TextView1;
//“提交按钮”
Button m_Button1;
//4个多选项
CheckBox m_CheckBox1;
CheckBox m_CheckBox2;
CheckBox m_CheckBox3;
CheckBox m_CheckBox4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_TextView1 = (TextView) findViewById(R.id.TextView1);
m_Button1 = (Button) findViewById(R.id.button1);
/* 取得每个CheckBox对象 */
m_CheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
m_CheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
m_CheckBox3 = (CheckBox) findViewById(R.id.CheckBox3);
m_CheckBox4 = (CheckBox) findViewById(R.id.CheckBox4);
m_CheckBox1.setChecked(true);
m_CheckBox4.setChecked(true);
//对每个选项设置事件监听
m_CheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(m_CheckBox1.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox1.getText());
}
}
});
////////////////////
m_CheckBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(m_CheckBox2.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox2.getText());
}
}
});
/////////////////
m_CheckBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(m_CheckBox3.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox3.getText());
}
}
});
////////////////
m_CheckBox4.setOnCheckedChangeListener(new MyClass());//对第4个checkbox变换一下写法练习一下
//对按钮设置事件监听
m_Button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
int num = 0;
if(m_CheckBox1.isChecked())
{
num++;
}
if(m_CheckBox2.isChecked())
{
num++;
}
if(m_CheckBox3.isChecked())
{
num++;
}
if(m_CheckBox4.isChecked())
{
num++;
}
DisplayToast("谢谢参与!你一共选择了"+num+"项!");
}
});
}
class MyClass implements CheckBox.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(m_CheckBox4.isChecked())
{
DisplayToast("你选择了:"+m_CheckBox4.getText());
}
else
{
DisplayToast("我晕,你怎么不选这项了! "+m_CheckBox4.getText());
}
}
}
/* 显示Toast */
public void DisplayToast(String str)
{
Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
//设置toast显示的位置
toast.setGravity(Gravity.TOP, 0, 220);
//显示该Toast
toast.show();
}
}
相关文章推荐
- android应用开发揭秘examples_04-09笔记(Spinner的使用,略有修改)
- android应用开发揭秘examples_04-10笔记(AutoCompleteTextView和MultiAutoCompleteTextView使用)
- android应用开发揭秘examples_04-12笔记(Button的使用)
- android应用开发揭秘examples_04-16笔记(ImageButton的使用)
- android应用开发揭秘examples_04-17笔记(Gallery的使用)
- android应用开发揭秘examples_04-11笔记(DatePicker,TimePicker时间日期设置,略有改动)
- android应用开发揭秘examples_04-13笔记(Menu的2种实现方式)
- android应用开发揭秘例4-6笔记(EditText的使用)
- android应用开发揭秘例example4-7笔记(RadioGroup和RadioButton的使用与改进)
- Android应用开发学习笔记04--使用布局
- android应用开发揭秘之examples_04_24(ProgressDialog对话框中的进度条)
- android应用开发揭秘之examples_04_29(TabWidget及FrameLayout)
- android应用开发揭秘例4-3笔记(TextView的使用)
- android应用开发揭秘例4-4笔记(ListView及Adapter的使用)
- android 应用开发:android studio使用笔记
- Android应用开发揭秘第七章笔记(二)
- android应用开发笔记---_Handler的使用
- android应用开发笔记---常用控件使用方法
- android应用开发笔记---SQLite使用
- android 应用开发揭秘,ndk使用过程总结