Android手机设计之:CheckBox

首先我们要先设计页面.

 

这里我们用的是CheckBox

 

string.xml Code

 

<? xml  version = "1.0" encoding = "utf-8" ?>
< resources >
    < string name = "hello" > 调查 : 你喜欢 Android 的原因 ! </ string >
    < string name = "app_name" > AndroidCheckBox </ string >
    < string name = "CheckBox1" > 无界限的应用程序 </ string >
    < string name = "CheckBox2" > 应用程序是在平等的条件下创建的 </ string >
    < string name = "CheckBox3" > 应用程序是可以轻松的嵌入网络 </ string >
    < string name = "CheckBox4" > 应用程序可以并行运行 </ string >

</ resources >

 

 

然后设计main.xml 文件

 

这里,创建了四个CheckBox以及一个Button按钮


<? 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/TextView1"

    android:layout_width = "fill_parent"

    android:layout_height = "wrap_content"

    android:text = "@string/hello"

    />

< CheckBox

    android:id = "@+id/CheckBox1"

    android:layout_width = "fill_parent"

    android:layout_height = "wrap_content"

    android:text = "@string/CheckBox1"

></ CheckBox >

< CheckBox

    android:id = "@+id/CheckBox2"

    android:layout_width = "fill_parent"

    android:layout_height = "wrap_content"

    android:text = "@string/CheckBox2"

></ CheckBox >

< CheckBox

    android:id = "@+id/CheckBox3"

    android:layout_width = "fill_parent"

    android:layout_height = "wrap_content"

    android:text = "@string/CheckBox3"

></ CheckBox >

< CheckBox

    android:id = "@+id/CheckBox4"

    android:layout_width = "fill_parent"

    android:layout_height = "wrap_content"

    android:text = "@string/CheckBox4"

></ CheckBox >

< Button

    android:id = "@+id/button1"

    android:layout_width = "wrap_content"

    android:layout_height = "wrap_content"

    android:text = " 提交 "

>  

</ Button >

 

</ LinearLayout > 

 

 

设计好页之后就开始编写代码了.

 

package com.android.checkbox;

 

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 AndroidCheckBox extends Activity {

    // 显示题目

    TextView m_TextView1 ;

    // 提交按钮

    Button m_Button1 ;

    // 四个多选项

    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_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 .setOnCheckedChangeListener( new CheckBox.OnCheckedChangeListener(){

 

           public void onCheckedChanged(CompoundButton buttonView,

                  boolean isChecked) {

              // TODO Auto-generated method stub

              if ( m_CheckBox1 .isChecked())

              {

                  DisplayToast( " 你选择了 :" + m_CheckBox1 .getText());

              }

           }

       

        });

        m_CheckBox2 .setOnCheckedChangeListener( new CheckBox.OnCheckedChangeListener(){

 

           public void onCheckedChanged(CompoundButton buttonView,

                  boolean isChecked) {

              // TODO Auto-generated method stub

              if ( m_CheckBox1 .isChecked())

              {

                  DisplayToast( " 你选择了 :" + m_CheckBox2 .getText());

              }

           }

       

        });

        m_CheckBox3 .setOnCheckedChangeListener( new CheckBox.OnCheckedChangeListener(){

 

           public void onCheckedChanged(CompoundButton buttonView,

                  boolean isChecked) {

              // TODO Auto-generated method stub

              if ( m_CheckBox1 .isChecked())

              {

                  DisplayToast( " 你选择了 :" + m_CheckBox3 .getText());

              }

           }

       

        });

        m_CheckBox4 .setOnCheckedChangeListener( new CheckBox.OnCheckedChangeListener(){

 

           public void onCheckedChanged(CompoundButton buttonView,

                  boolean isChecked) {

              // TODO Auto-generated method stub

              if ( m_CheckBox1 .isChecked())

              {

                  DisplayToast( " 你选择了 :" + m_CheckBox4 .getText());

              }

           }

       

        });

        // 对按钮设置监听事件

        m_Button1 .setOnClickListener( new Button.OnClickListener(){

 

           public void onClick(View arg0) {

              // TODO Auto-generated method stub

              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+ " " );

           }

       

        });

    }

    // 显示 Toast

    public void DisplayToast(String str)

    {

    Toast toast = Toast.makeText ( this , str,Toast. LENGTH_SHORT );

    // 设置显示的位置

    toast.setGravity(Gravity. TOP , 0, 200);

    // 显示 Toast

    toast.show();

    }

}

 

 

 

呃第一次写Blog.如果有不好的地方请大家伙指正.谢谢了.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值