在本教程中,咱們將向您展現如何建立XML文件中的3個複選框,並論證了聽者的使用檢查–選中或取消選中複選框的狀態。html
P.S這個項目是在Eclipse 3.7開發,並與Android 2.3.3測試。java
1。自定義字符串
Open 「res/values/strings.xml」 file, add some user-defined string.android
res/values/strings.xml文件:ios
<?xml version="1.0" encoding="utf-8"?>
<resources>windows
<string 姓名=「hello「>Hello World, MyAndroidAppActivity!</string> <string 姓名=「app_name「>MyAndroidApp</string> <string 姓名=「chk_ios「>IPhone</string> <string 姓名=「chk_android「>Android</string> <string 姓名=「chk_windows「>Windows Mobile</string> <string 姓名=「btn_display「>Display</string>
</resources>
2。複選框
Open 「res/layout/ main.xml」 file, add 3 「複選框」 and a button, inside the 線性佈局.app
文件:res/layout/ main.xmlide
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=「http://schemas.android.com/ap...「函數
安卓layout_width=「fill_parent「 安卓layout_height=「fill_parent「 安卓orientation=「vertical「 > <CheckBox 安卓id=「@+id/chkIos「 安卓layout_width=「wrap_content「 安卓layout_height=「wrap_content「 安卓text=「@string/chk_ios「 > <CheckBox 安卓id=「@+id/chkAndroid「 安卓layout_width=「wrap_content「 安卓layout_height=「wrap_content「 安卓text=「@string/chk_android「 安卓checked=「true「 > <CheckBox 安卓id=「@+id/chkWindows「 安卓layout_width=「wrap_content「 安卓layout_height=「wrap_content「 安卓text=「@string/chk_windows「 > <Button 安卓id=「@+id/btnDisplay「 安卓layout_width=「wrap_content「 安卓layout_height=「wrap_content「 安卓text=「@string/btn_display「 >
</LinearLayout>
使複選框默認被選中
Put android:checked="true" inside checkbox element to make it checked bu default. In this case, 「Android」 option is checked by default.
三.代碼代碼
Attach listeners inside your activity 「onCreate()」 method, to monitor following events :佈局
If checkbox id : 「chkios」 is checked, display a floating box with message 「Bro, try Android」.
若是按鈕被點擊時,顯示一個浮動框和複選框的狀態顯示。
文件:myandroidappactivity.java測試
旅行包 com。mkyong。android
進口 android。app。Activity
進口 android。os。Bundle
進口 android。view。View
進口 android。view。View。OnClickListener
進口 android。widget。Button
進口 android。widget。CheckBox
進口 android。widget。Toast
公共 類 myandroidappactivity 延伸 活動 {
私人 CheckBox chkIos, chkAndroid, chkWindows
私人 Button btnDisplay
@Override
公共 無效 建立時的回調函數(Bundle savedInstanceState) {
超級的。建立時的回調函數(savedInstanceState) setContentView(R。layout。main) addlisteneronchkios() addlisteneronbutton()
}
公共 無效 addlisteneronchkios() {
chkIos = (CheckBox) findViewById(R。id。chkIos) chkIos。setonclicklistener(新 onclicklistener() { @Override 公共 無效 onclick(View v) { / /是chkios檢查嗎? 若是 (((CheckBox) v)。把關()) { Toast。maketext(MyAndroidAppActivity。這, 「兄弟,嘗試Android:)」, Toast。LENGTH_LONG)。商展() } } })
}
公共 無效 addlisteneronbutton() {
chkIos = (CheckBox) findViewById(R。id。chkIos) chkAndroid = (CheckBox) findViewById(R。id。chkAndroid) chkWindows = (CheckBox) findViewById(R。id。chkWindows) btnDisplay = (Button) findViewById(R。id。btnDisplay) btnDisplay。setonclicklistener(新 onclicklistener() { clicked button is when /運行 @Override 公共 無效 onclick(View v) { StringBuffer result = 新 StringBuffer() result。追加(「iPhone檢查:」)。追加(chkIos。把關()) result。追加(「nandroid檢查:」)。追加(chkAndroid。把關()) result。追加(「移動nwindows檢查:\」)。追加(chkWindows。把關()) Toast。maketext(MyAndroidAppActivity。這, result。toString(), Toast.LENGTH_LONG).show(); } });
}
}
Run the application.
1. Result :
2. If 「IPhone」 is checked :
3. Checked 「IPhone」 and 「Windows Mobile」, later, click on the 「display」 button :