day03:CheckBox复选框
案例:
选择语言,按确定后,汇总选择的语言
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lvshitech.checkboxdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lvshitech.checkboxdemo.CheckBoxDemo" 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>
CheckBoxDemo.java
package com.lvshitech.checkboxdemo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; public class CheckBoxDemo extends Activity { // 页面控件 CheckBox chkChs, chkEns, chkJap; Button btnOk; TextView tvResult; // 各个复选框是否被选中的标识 boolean chkChsFlag=false, chkEnsFlag=false, chkJapFlag=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设定布局 setContentView(R.layout.checkboxdemo); // 关联XML chkChs = (CheckBox) findViewById(R.id.chkChs); chkEns = (CheckBox) findViewById(R.id.chkEns); chkJap = (CheckBox) findViewById(R.id.chkJap); btnOk = (Button) findViewById(R.id.btnOk); tvResult = (TextView) findViewById(R.id.tvResult); // 中文复选框监听事件 chkChs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { chkChsFlag = true; } else { chkChsFlag = false; } } }); // 英语复选框监听事件 chkEns.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { chkEnsFlag = true; } else { chkEnsFlag = false; } } }); // 日语复选框监听事件 chkJap.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { chkJapFlag = true; } else { chkJapFlag = false; } } }); // 确定按钮监听事件 btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String result = ""; StringBuffer sb = new StringBuffer(); if(chkChsFlag) { sb.append(chkChs.getText() + "、"); } if(chkEnsFlag) { sb.append(chkEns.getText() + "、"); } if(chkJapFlag) { sb.append(chkJap.getText() + "、"); } if(!"".equals(sb.toString())) { String temp = sb.toString().trim(); result = "选中的语言:" + temp.substring(0, temp.length()-1); } else { result = "没有选中任何语言!"; } tvResult.setText(result); tvResult.setTextColor(Color.RED); } }); } }
checkboxdemo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tvLanguage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/language" android:textSize="16sp" /> <CheckBox android:id="@+id/chkChs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvLanguage" android:layout_below="@+id/tvLanguage" android:layout_marginTop="20dp" android:text="@string/chinese" /> <CheckBox android:id="@+id/chkEns" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/chkChs" android:layout_below="@+id/chkChs" android:layout_marginTop="18dp" android:text="@string/english" /> <CheckBox android:id="@+id/chkJap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/chkEns" android:layout_below="@+id/chkEns" android:layout_marginTop="22dp" android:text="@string/japanese" /> <Button android:id="@+id/btnOk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/chkJap" android:layout_alignBottom="@+id/chkJap" android:layout_alignRight="@+id/tvResult" android:layout_marginRight="16dp" android:text="@string/btnOk" /> <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/chkJap" android:layout_below="@+id/btnOk" android:layout_marginTop="56dp" /> </RelativeLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">CheckBoxDemo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="language">您要学习的语言:</string> <string name="chinese">中文</string> <string name="english">英语</string> <string name="japanese">日语</string> <string name="btnOk">确定</string> </resources>