Android之複選框CheckBox使用

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:layout_width="wrap_content<span id="transmark"></span>" android:layout_height="wrap_content"
            android:id="@+id/checkbox_test"
            android:text="@string/str_checkbox_view"/>
    <CheckBox android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:id="@+id/myCheckBox01"
            android:text="@string/str_checkbox01"/>
    <CheckBox android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:id="@+id/myCheckBox02"
            android:text="@string/str_checkbox02"/>
    <CheckBox android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:id="@+id/myCheckBox03"
            android:text="@string/str_checkbox03"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/checkbox_view"/>
</LinearLayout>


strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">myRadio</string>
    <string name="str_checkbox_view">CheckBox test: </string>
    <string name="str_checkbox01">checkbox_01</string>
    <string name="str_checkbox02">checkbox_02</string>
    <string name="str_checkbox03">checkbox_03</string>
</resources>

MyActivity.java

package com.example.myRadio;

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

public class MyActivity extends Activity {

    private TextView mTextView01;
    private CheckBox mCheckBox01;
    private CheckBox mCheckBox02;
    private CheckBox mCheckBox03;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //bind Element
        mTextView01 = (TextView)findViewById(R.id.checkbox_view);
        mCheckBox01 = (CheckBox)findViewById(R.id.myCheckBox01);
        mCheckBox02 = (CheckBox)findViewById(R.id.myCheckBox02);
        mCheckBox03 = (CheckBox)findViewById(R.id.myCheckBox03);

        mTextView01.setText("your choose are: ");

        //bind Listener
        mCheckBox01.setOnCheckedChangeListener(onCheckedChangeListener);
        mCheckBox02.setOnCheckedChangeListener(onCheckedChangeListener);
        mCheckBox03.setOnCheckedChangeListener(onCheckedChangeListener);
    }

    private CheckBox.OnCheckedChangeListener onCheckedChangeListener = new CheckBox.OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
            String str0 = "the choose are:";
            String str1 = getString(R.string.str_checkbox01);
            String str2 = getString(R.string.str_checkbox02);
            String str3 = getString(R.string.str_checkbox03);

            if (mCheckBox01.isChecked()){
                str0 = str0 + str1 + " ";
            }
            if (mCheckBox02.isChecked()){
                str0 = str0 + str2 + " ";
            }
            if (mCheckBox03.isChecked()){
                str0 = str0 + str3;
            }
            mTextView01.setText(str0);
        }
    };
}


發佈了35 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章