Android控件之-CheckBox

多项选择的组件主要是由CheckBox组成的

package com.ko8e;

import android.app.Activity;
import android.content.Intent;
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 {
    /** Called when the activity is first created. */
	private TextView textView = null;
	private Button button = null;
	private CheckBox box1 = null;
	private CheckBox box2 = null;
	private CheckBox box3 = null;
	private CheckBox box4 = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textView = (TextView) findViewById(R.id.view);
        textView.setText(R.string.hello);
        button = (Button) findViewById(R.id.button);
        button.setText(R.string.button);
        box1 =(CheckBox) findViewById(R.id.box1);
        box2 =(CheckBox) findViewById(R.id.box2);
        box3 =(CheckBox) findViewById(R.id.box3);
        box4 =(CheckBox) findViewById(R.id.box4);
        
        box1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {				
				if(box1.isChecked()) {
					DisplayToast("你选择了:" + box1.getText().toString());
				}
			}
        });
        box2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {				
				if(box2.isChecked()) {
					DisplayToast("你选择了:" + box2.getText().toString());
				}
			}
        });
        box3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {				
				if(box3.isChecked()) {
					DisplayToast("你选择了:" + box3.getText().toString());
				}
			}
        });
        box4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {				
				if(box4.isChecked()) {
					DisplayToast("你选择了:" + box4.getText().toString());
				}
			}
        });
        
        button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				int num = 0;
				Intent intent = new Intent();
				if(box1.isChecked()) {
					num++;
					intent.putExtra("box1", box1.getText().toString());
				}
				if(box2.isChecked()) {
					num++;
					intent.putExtra("box2", box2.getText().toString());
				}
				if(box3.isChecked()) {
					num++;
					intent.putExtra("box3", box3.getText().toString());
				}
				if(box4.isChecked()) {
					num++;
					intent.putExtra("box4", box4.getText().toString());
				}
				intent.putExtra("num", num);
				intent.setClass(Activity01.this, OtherActivityForView.class);
				Activity01.this.startActivity(intent);
				
				//DisplayToast("你一共选择了" +num + "项");
			}
        });
    }
	public void DisplayToast(String str) {
		Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP, 0, 200);
		toast.show();
	}
	
}

另一个Activity:

package com.ko8e;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivityForView extends Activity {

	private TextView view = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		Intent intent = getIntent();
		String b1 = intent.getStringExtra("box1");
		String b2 = intent.getStringExtra("box2");
		String b3 = intent.getStringExtra("box3");
		String b4 = intent.getStringExtra("box4");
		String n = intent.getStringExtra("num");
		view = (TextView) findViewById(R.id.otherview);
		view.setText("你选择了:" + n + "项" + "\n" + "分别是:"+ "\n" + b1 + "\n"+ b2+ "\n" + b3 + "\n"+ b4);
	}
}

layout中的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/view"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<CheckBox
	android:id="@+id/box1"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/huang"
	/>
<CheckBox
	android:id="@+id/box2"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/lyq"
	/>
<CheckBox
	android:id="@+id/box3"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/php"
	/>
<CheckBox
	android:id="@+id/box4"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/me"
	/>
<Button
	android:id="@+id/button"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	/>
</LinearLayout>

other.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/otherview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>

最后记得把第二个Activity添加到Manifest.xml文件中:

<activity android:name=".OtherActivityForView"
			android:label="@string/app_name"
			/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值