android CheckBox(多项选择)

CheckBox01.JAVA:

 

package com.CheckBox01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CheckBox01 extends Activity {
 private TextView mTextView;
 private Button mButton;
 private CheckBox mCB1,mCB2,mCB3,mCB4;
 
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mTextView=(TextView)findViewById(R.id.mTextView);
        mButton=(Button)findViewById(R.id.mButton);
        mCB1=(CheckBox)findViewById(R.id.mCB1);
        mCB2=(CheckBox)findViewById(R.id.mCB2);
        mCB3=(CheckBox)findViewById(R.id.mCB3);
        mCB4=(CheckBox)findViewById(R.id.mCB4);
       
        mCB1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         
         @Override
         public void onCheckedChanged(CompoundButton buttonView,
           boolean isChecked){
          if(mCB1.isChecked()){
           DisplayToast("you choose:"+mCB1.getText());
          }
         }
        });
       
        mCB2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         
         @Override
         public void onCheckedChanged(CompoundButton buttonView,
           boolean isChecked){
          if(mCB2.isChecked()){
           DisplayToast("you choose:"+mCB2.getText());
          }
         }
        });
       
        mCB3.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         
         @Override
         public void onCheckedChanged(CompoundButton buttonView,
           boolean isChecked){
          if(mCB3.isChecked()){
           DisplayToast("you choose:"+mCB3.getText());
          }
         }
        });
       
        mCB4.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         
         @Override
         public void onCheckedChanged(CompoundButton buttonView,
           boolean isChecked){
          if(mCB4.isChecked()){
           DisplayToast("you choose:"+mCB4.getText());
          }
         }
        });
       
        mButton.setOnClickListener(new OnClickListener(){
         public void onClick(View v){
          if(mCB1.isChecked()&&mCB2.isEnabled()){
           DisplayToast("GOOD!!!!");
          } 
          
         }
        });
    }
    public void DisplayToast(String str){
     Toast toast=Toast.makeText(this, str, Toast.LENGTH_SHORT);
     toast.show();
    }
}

 

 

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/mTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button
 android:id="@+id/mButton"
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mButton"
    />
<CheckBox
 android:id="@+id/mCB1"
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mCB1"
    />
<CheckBox
 android:id="@+id/mCB2"
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mCB2"
    />
<CheckBox
 android:id="@+id/mCB3"
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mCB3"
    />
<CheckBox
 android:id="@+id/mCB4"
 android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mCB4"
    />
</LinearLayout>

 

 

AndroidMainfest.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.CheckBox01"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CheckBox01"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值