Android checkBox简单使用

来源:互联网 发布:淘宝消除模块间隙代码 编辑:程序博客网 时间:2024/07/17 21:13

strings.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Checkbox</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>        <string name = "shopping_list">网上购物清单</string>    <string name="yourChocieWoods_text" >您选择购买的商品:</string><string name="woods_Text_MP4">纽曼MP4</string><string name="woods_Text_musicCD">Beyond乐队CD</string><string name="woods_Text_book">Android程序员指南</string></resources>


main.xml

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin" >    <TextViewandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/TextView_shoppingList"android:text="@string/shopping_list"android:textSize="25px"/><CheckBoxandroid:layout_height="wrap_content"android:id="@+id/CheckBox_MP4"android:text="@string/woods_Text_MP4"android:layout_width="wrap_content"/><CheckBoxandroid:layout_height="wrap_content"android:text="@string/woods_Text_musicCD"android:id="@+id/CheckBox_musicCD"android:layout_width="wrap_content"/><CheckBoxandroid:layout_height="wrap_content"android:text="@string/woods_Text_book"android:id="@+id/CheckBox_book"android:layout_width="wrap_content"/><TextViewandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/TextView_yourChoice"android:text="@string/yourChocieWoods_text"android:textSize="20px"/><TextViewandroid:layout_height="wrap_content"android:id="@+id/TextView_yourWoodsList"android:layout_width="fill_parent"android:textSize="20px"/></LinearLayout>


MainActivity.java

package com.lianxi.checkbox;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.TextView;public class MainActivity extends Activity {private TextView yourChooseList;private CheckBox mp4Box, cdBox, bookBox;private String showStr;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);yourChooseList = (TextView) findViewById(R.id.TextView_yourWoodsList);mp4Box = (CheckBox) findViewById(R.id.CheckBox_MP4);cdBox = (CheckBox) findViewById(R.id.CheckBox_musicCD);bookBox = (CheckBox) findViewById(R.id.CheckBox_book);showStr = getString(R.string.yourChocieWoods_text);mp4Box.setOnCheckedChangeListener(myOnCheckedChangeListener);cdBox.setOnCheckedChangeListener(myOnCheckedChangeListener);bookBox.setOnCheckedChangeListener(myOnCheckedChangeListener);}private OnCheckedChangeListener myOnCheckedChangeListener = new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// 动态显示选中结果if(mp4Box.isChecked()){showStr = getString(R.string.woods_Text_MP4) + "\n";showBuy();}else if(cdBox.isChecked()){showStr = getString(R.string.woods_Text_musicCD) + "\n";showBuy();}else if(bookBox.isChecked()){showStr = getString(R.string.woods_Text_book) + "\n";showBuy();}}};public void showBuy(){yourChooseList.setText(showStr);}}

运行结果如下所示: