Android之CheckBox(多選按鈕)

多選按鈕CheckBox的可以實現多項選擇,我們可以現在佈局文件中定義多選按鈕,然後對每一個多選按鈕進行事件監聽
setOnCheckedChangeListener,通過isChecked來判斷選項是否被選中

下面是一個例子,可以很好的理解CheckBox的使用
CheckBoxTest.java

package org.loulijun.checkbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class CheckBoxTest extends Activity {
    /** Called when the activity is first created. */
    TextView textview;
    Button submit;
    CheckBox checkbox1;
    CheckBox checkbox2;
    CheckBox checkbox3;
    CheckBox checkbox4;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textview=(TextView)findViewById(R.id.textview);
        submit=(Button)findViewById(R.id.submit);
        
        //取得每一個CheckBox對象
        checkbox1=(CheckBox)findViewById(R.id.checkbox1);
        checkbox2=(CheckBox)findViewById(R.id.checkbox2);
        checkbox3=(CheckBox)findViewById(R.id.checkbox3);
        checkbox4=(CheckBox)findViewById(R.id.checkbox4);
        
        //爲每一個選項設置監聽
        checkbox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
        {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                        boolean isChecked) {
                                // TODO Auto-generated method stub
                                if(checkbox1.isChecked())
                                {
                                        DisplayToast("你選擇了:"+checkbox1.getText());
                                }
                        }
                
        });
        
        checkbox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
        {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                        boolean isChecked) {
                                // TODO Auto-generated method stub
                                if(checkbox2.isChecked())
                                {
                                        DisplayToast("你選擇了:"+checkbox2.getText());
                                }
                        }
                
        });
        
        checkbox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
        {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                        boolean isChecked) {
                                // TODO Auto-generated method stub
                                if(checkbox3.isChecked())
                                {
                                        DisplayToast("你選擇了:"+checkbox3.getText());
                                }
                        }
                
        });
        
        checkbox4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
        {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                        boolean isChecked) {
                                // TODO Auto-generated method stub
                                if(checkbox4.isChecked())
                                {
                                        DisplayToast("你選擇了:"+checkbox4.getText());
                                }
                        }
                
        });
        
        submit.setOnClickListener(new Button.OnClickListener()
        {
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                int num=0;
                                if(checkbox1.isChecked())
                                {
                                        num++;
                                }
                                if(checkbox2.isChecked())
                                {
                                        num++;
                                }
                                if(checkbox3.isChecked())
                                {
                                        num++;
                                }
                                if(checkbox4.isChecked())
                                {
                                        num++;
                                }
                                DisplayToast("您一共選擇了"+num+"款Android手機!");
                        }
                
        });
    }
    public void DisplayToast(String str)
    {
            Toast toast=Toast.makeText(this, str, Toast.LENGTH_SHORT);
            //設置Toast的顯示位置
            toast.setGravity(Gravity.TOP, 0, 220);
            //顯示Toast
            toast.show();
    }
}



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/textview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/checkbox1"
/>
<CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/checkbox2"
/>
<CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/checkbox3"
/>
<CheckBox
        android:id="@+id/checkbox4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/checkbox4"
/>
<Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="提交信息"
/>
</LinearLayout>



strings.xml文件

Java代碼

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">調查:你喜歡Android的那款手機?</string>
    <string name="app_name">CheckBoxTest</string>
    <string name="checkbox1">HTC desire HD</string>
    <string name="checkbox2">Google nexus one</string>
    <string name="checkbox3">HTC defy</string>
    <string name="checkbox4">摩托羅拉里程碑II</string>
</resources>



運行結果如下:

212030em3lllcc3e5fzly0.gif 212030q1qj4wq1zy3i4qn5.gif212028sqq40ko60v4yzmyz.gif

原文參考:http://www.apkbus.com/forum.php?mod=viewthread&tid=14270 

此整理拷貝資料,轉載請標明出處,謝謝!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章