Android基礎教程(六)之----多選項CheckBox的綜合應用

你們好,咱們這一節將講多選項CheckBox 的綜合應用,咱們的程序主要構造兩個CheckBox 的對象,以及一個TextView對象,並經過setOnCheckedChangeLisener 實現onCheckedChanged ()方法來更新TextView 文字. 

首先咱們看一下效果圖:







下面是主程序的代碼:

string.xml:
android

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, CheckboxDemo!</string>
  4.     <string name="app_name">CheckboxDemo</string>
  5.     <string name="hobby">你的愛好是:</string>
  6.     <string name="basketball">籃球</string>
  7.     <string name="football">足球</string>
  8. </resources>
複製代碼
主程序界面代碼main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView 
  8.     android:id="@+id/textview1"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     android:text="@string/hobby"
  12.     />
  13. <CheckBox
  14.     android:id="@+id/checkbox1"
  15.     android:layout_width="wrap_content"
  16.     android:layout_height="wrap_content"
  17.     android:text="@string/basketball"
  18. />
  19. <CheckBox
  20.     android:id="@+id/checkbox2"
  21.     android:layout_width="wrap_content"
  22.     android:layout_height="wrap_content"
  23.     android:text="@string/football"
  24. />
  25. </LinearLayout>
複製代碼
最後是程序的核心代碼CheckBoxDemo:
  1. package com.android.test;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.CheckBox;
  5. import android.widget.CompoundButton;
  6. import android.widget.TextView;

  7. public class CheckboxDemo extends Activity {
  8.   
  9.     private TextView tv;
  10.     private CheckBox cb1;
  11.     private CheckBox cb2;
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.main);
  15.       
  16.         tv = (TextView)findViewById(R.id.textview1);
  17.         cb1 = (CheckBox)findViewById(R.id.checkbox1);
  18.         cb2 = (CheckBox)findViewById(R.id.checkbox2);
  19.       
  20.         cb1.setOnCheckedChangeListener(cbListener);
  21.         cb2.setOnCheckedChangeListener(cbListener);
  22.       
  23.       
  24.     }
  25.   
  26.     private CheckBox.OnCheckedChangeListener cbListener =
  27.         new CheckBox.OnCheckedChangeListener(){
  28.       
  29.         public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
  30.         {
  31.             String stv = getString(R.string.hobby);
  32.             String scb1 = getString(R.string.basketball);
  33.             String scb2 = getString(R.string.football);
  34.             //判斷一共有四種狀況
  35.             if(cb1.isChecked()== true && cb2.isChecked()== true)
  36.             {
  37.                 tv.setText(stv + scb1 + "," + scb2);
  38.             }
  39.             else if(cb1.isChecked()== true && cb2.isChecked()== false)
  40.             {
  41.                 tv.setText(stv+scb1);
  42.             }
  43.             else if(cb1.isChecked() == false && cb2.isChecked() == true)
  44.             {
  45.                 tv.setText(stv+scb2);
  46.             }
  47.             else{
  48.                 tv.setText(stv);
  49.             }
  50.         }
  51.     };
  52.   
  53. }
複製代碼
相關文章
相關標籤/搜索