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

來源:互聯網
上載者:User

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

 

首先我們看一下:

 

 

 

 

下面是主程式的代碼:


string.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, CheckboxDemo!</string>
    <string name="app_name">CheckboxDemo</string>
    <string name="hobby">你的愛好是:</string>
    <string name="basketball">籃球</string>
    <string name="football">足球</string>


</resources>

 

主程式介面代碼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:id="@+id/textview1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hobby"
    />
<CheckBox
    android:id="@+id/checkbox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/basketball"
/>
<CheckBox
    android:id="@+id/checkbox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/football"
/>


</LinearLayout>

 

最後是程式的核心代碼CheckBoxDemo:

 

package com.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class CheckboxDemo extends Activity {
  
    private TextView tv;
    private CheckBox cb1;
    private CheckBox cb2;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        tv = (TextView)findViewById(R.id.textview1);
        cb1 = (CheckBox)findViewById(R.id.checkbox1);
        cb2 = (CheckBox)findViewById(R.id.checkbox2);
       
        cb1.setOnCheckedChangeListener(cbListener);
        cb2.setOnCheckedChangeListener(cbListener);


       
       
    }
   
    private CheckBox.OnCheckedChangeListener cbListener =
        new CheckBox.OnCheckedChangeListener(){
       
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
        {
            String stv = getString(R.string.hobby);
            String scb1 = getString(R.string.basketball);
            String scb2 = getString(R.string.football);
            //判斷一共有四種情況
            if(cb1.isChecked()== true && cb2.isChecked()== true)
            {
                tv.setText(stv + scb1 + "," + scb2);
            }
            else if(cb1.isChecked()== true && cb2.isChecked()== false)
            {
                tv.setText(stv+scb1);
            }
            else if(cb1.isChecked() == false && cb2.isChecked() == true)
            {
                tv.setText(stv+scb2);
            }
            else{
                tv.setText(stv);
            }
        }
    };


   
}

 

這一節就至此為止,由於時間的原因,我就不加註釋了,有什麼不能理解的地方,請大家留言,我會詳細解答.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.