4.5 CheckBox应用

本文介绍了一个使用Android CheckBox组件的应用示例。示例中通过监听CheckBox的状态变化更新TextView的内容,展示了如何根据用户的选择显示不同的提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.chaowen;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class Ex04_05_checkBox2 extends Activity {
    /** Called when the activity is first created. */
	private TextView mTextView1;
	private CheckBox mCheckBox1;
	private CheckBox mCheckBox2;
	private CheckBox mCheckBox3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //通过findViewById取得TextView对象并调整文字内容
        mTextView1=(TextView)findViewById(R.id.myTextView1);
        mTextView1.setText("你所选择的项目有:");
        
        //通过findViewById取得三个CheckBox对象
        mCheckBox1=(CheckBox)findViewById(R.id.myCheckBox1);
        mCheckBox2=(CheckBox)findViewById(R.id.myCheckBox2);
        mCheckBox3=(CheckBox)findViewById(R.id.myCheckBox3);
        
       
        
        //声明并构造onCheckedChangeListener对象
        CheckBox.OnCheckedChangeListener mcheckBoxChanged=new CheckBox.OnCheckedChangeListener(){
        	@Override
        	public void onCheckedChanged(CompoundButton buttonView,
        			boolean isChecked) {
        		//通过getString()取得CheckBox的文字字符串
        		String str0="所选的项目为: ";
        		String str1=getString(R.string.str_checkbox1);
        		String str2=getString(R.string.str_checkbox2);
        		String str3=getString(R.string.str_checkbox3);
        		String plus=";";
        		String result="但是超过预算咯!!";
        		String result2="还可以再多买几本喔!!";
        		
        		//任一CheckBox被勾选后,该CheckBox的文字会改变TextView的文字内容
        		if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==true
        	              & mCheckBox3.isChecked()==true) 
        	          { 
        	            mTextView1.setText(str0+str1+plus+str2+plus+str3+result);
        	          } 
        	          else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==true
        	              & mCheckBox3.isChecked()==true) 
        	          { 
        	            mTextView1.setText(str0+str2+plus+str3+result);
        	          } 
        	          else if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==false
        	              & mCheckBox3.isChecked()==true) 
        	          { 
        	            mTextView1.setText(str0+str1+plus+str3+result);
        	          } 
        	          else if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==true
        	              & mCheckBox3.isChecked()==false) 
        	          { 
        	            mTextView1.setText(str0+str1+plus+str2+result);
        	          } 
        	          else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==false
        	              & mCheckBox3.isChecked()==true) 
        	          { 
        	            mTextView1.setText(str0+str3+plus+result2);
        	          } 
        	          else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==true
        	              & mCheckBox3.isChecked()==false) 
        	          { 
        	            mTextView1.setText(str0+str2);
        	          } 
        	          else if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==false
        	              & mCheckBox3.isChecked()==false) 
        	          { 
        	            mTextView1.setText(str0+str1);
        	          } 
        	          else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==false
        	              & mCheckBox3.isChecked()==false) 
        	          { 
        	            mTextView1.setText(str0);
        	          } 
        	}
        };
        
        //设置OnCheckedChangeListener给三个Checkbox对象
        mCheckBox1.setOnCheckedChangeListener(mcheckBoxChanged);
        mCheckBox2.setOnCheckedChangeListener(mcheckBoxChanged);
        mCheckBox3.setOnCheckedChangeListener(mcheckBoxChanged);
    }
}

 

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/myTitle"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/str_title"
>
</TextView>
<CheckBox
  android:id="@+id/myCheckBox1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/str_checkbox1"
>
</CheckBox>
<CheckBox
  android:id="@+id/myCheckBox2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/str_checkbox2"
>
</CheckBox>
<CheckBox
android:id="@+id/myCheckBox3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_checkbox3"
>
</CheckBox>
<TextView
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
 

 Strings.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Ex04_05_checkBox2!</string>
    <string name="app_name">Ex04_05_checkBox2</string>
    <string name="str_title">我的消費券採購單</string>
  <string name="str_checkbox1">泰國機票一張</string>
  <string name="str_checkbox2">iPhone一台</string>
  <string name="str_checkbox3">Android整合應用範例集一本</string>
</resources>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值