Android应用开发UI(多选框)

Android平台使用CheckBox来实现多项选择。效果图如下:

 

代码:

1.res/values/string.xml

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title_tx">调查:你喜欢Android的原因:</string>
    <string name="app_name">A007</string>
    <string name="checkBox1">无界限的应用程序</string>
    <string name="checkBox2">应用程序是在平等的条件下创建的。</string>
    <string name="checkBox3">应用程序可以轻松的嵌入到网络。</string>
    <string name="checkBox4">应用程序可以并行运行。</string>
    <string name="btn_submit">提交</string>
    <string name="u_selected">你选择了:</string>
    <string name="i_tem">项。</string>
     <string name="about_80080088">更多详情,可登录http://www.80080088.com了解。</string>
</resources>

 

 

2.res/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:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/title_tx"
	    android:id="@+id/title_tx"/>
	<TextView 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content" 
		android:id="@+id/about_80080088">
	</TextView>
	<CheckBox 
		android:text="@string/checkBox1" 
		android:id="@+id/checkBox1" 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content">
	</CheckBox>
	<CheckBox 
		android:text="@string/checkBox2" 
		android:id="@+id/checkBox2" 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content">
	</CheckBox>
	<CheckBox 
		android:text="@string/checkBox3" 
		android:id="@+id/checkBox3" 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content">
	</CheckBox>
	<CheckBox 
		android:text="@string/checkBox4" 
		android:id="@+id/checkBox4" 
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content">
	</CheckBox>
	<Button 
		android:layout_width="wrap_content" 
		android:text="@string/btn_submit" 
		android:layout_height="wrap_content" 
		android:id="@+id/btn_submit">
	</Button>
</LinearLayout>

 

 

3,Activity类

package com.cottsoft.android;

import android.app.Activity;
import android.graphics.Color;
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 A007Activity extends Activity {
	private TextView m_TextView,about_80080088;
	private Button btn;
	private CheckBox m_CheckBox1,m_CheckBox2,m_CheckBox3,m_CheckBox4;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        m_TextView = (TextView) findViewById(R.id.title_tx);
        m_TextView.setTextColor(Color.BLUE);
        m_TextView.setTextSize(22);
        
        about_80080088 = (TextView) findViewById(R.id.about_80080088);
        about_80080088.setText(getResources().getString(R.string.about_80080088));
        about_80080088.setTextColor(Color.GRAY);
        about_80080088.setTextSize(15);
        
        btn = (Button) findViewById(R.id.btn_submit);
        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.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(m_CheckBox1.isChecked()){
					DisplayToast(getResources().getString(R.string.u_selected)+m_CheckBox1.getText());
				}
			}
        });
        
        m_CheckBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(m_CheckBox2.isChecked()){
					DisplayToast(getResources().getString(R.string.u_selected)+m_CheckBox2.getText());
				}
			}
        });
        
        m_CheckBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(m_CheckBox3.isChecked()){
					DisplayToast(getResources().getString(R.string.u_selected)+m_CheckBox3.getText());
				}
			}
        });
        
        m_CheckBox4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(m_CheckBox4.isChecked()){
					DisplayToast(getResources().getString(R.string.u_selected)+m_CheckBox4.getText());
				}
			}
        });
        
        btn.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View arg0) {
				int num = 0;
				if(m_CheckBox1.isChecked()){
					num++;
				}
				if(m_CheckBox2.isChecked()){
					num++;
				}
				if(m_CheckBox3.isChecked()){
					num++;
				}
				if(m_CheckBox4.isChecked()){
					num++;
				}
				DisplayToast(getResources().getString(R.string.u_selected)+num+getResources().getString(R.string.i_tem));
			}
        	
        });
    }
        
    public void DisplayToast(String str){
    	Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
    	toast.setGravity(Gravity.TOP, 0, 380);
    	toast.show();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值