android复选按钮,Android的复选框的详细开发案例分析
在本教程中,我們將向您展示如何創(chuàng)建XML文件中的3個(gè)復(fù)選框,并論證了聽者的使用檢查–選中或取消選中復(fù)選框的狀態(tài)。
P.S這個(gè)項(xiàng)目是在Eclipse 3.7開發(fā),并與Android 2.3.3測(cè)試。
1。自定義字符串
Open “res/values/strings.xml” file, add some user-defined string.
res/values/strings.xml文件:
<resources><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。復(fù)選框
Open “res/layout/ main.xml” file, add 3 “復(fù)選框” and a button, inside the 線性布局.
文件:res/layout/ main.xml
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android“安卓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>
使復(fù)選框默認(rèn)被選中
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”.
如果按鈕被點(diǎn)擊時(shí),顯示一個(gè)浮動(dòng)框和復(fù)選框的狀態(tài)顯示。
文件:myandroidappactivity.java
旅行包 com。mkyong。android
進(jìn)口 android。app。Activity
進(jìn)口 android。os。Bundle
進(jìn)口 android。view。View
進(jìn)口 android。view。View。OnClickListener
進(jìn)口 android。widget。Button
進(jìn)口 android。widget。CheckBox
進(jìn)口 android。widget。Toast
公共 類 myandroidappactivity 延伸 活動(dòng) {
私人 CheckBox chkIos, chkAndroid, chkWindows
私人 Button btnDisplay
@Override
公共 無效 創(chuàng)建時(shí)的回調(diào)函數(shù)(Bundle savedInstanceState) {超級(jí)的。創(chuàng)建時(shí)的回調(diào)函數(shù)(savedInstanceState)
setContentView(R。layout。main)
addlisteneronchkios()
addlisteneronbutton()
}
公共 無效 addlisteneronchkios() {chkIos?=?(CheckBox)?findViewById(R。id。chkIos)
chkIos。setlistener(新?listener()?{
@Override
公共?無效?(View?v)?{
/?/是chkios檢查嗎?
如果?(((CheckBox)?v)。把關(guān)())?{
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。setlistener(新?listener()?{
clicked?button?is?when?/運(yùn)行
@Override
公共?無效?(View?v)?{
StringBuffer?result?=?新?StringBuffer()
result。追加(“iPhone檢查:”)。追加(chkIos。把關(guān)())
result。追加(“nandroid檢查:”)。追加(chkAndroid。把關(guān)())
result。追加(“移動(dòng)nwindows檢查:\”)。追加(chkWindows。把關(guān)())
Toast。maketext(MyAndroidAppActivity。這,?result。toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
4. Demo
Run the application.
1. Result :
2. If “IPhone” is checked :
3. Checked “IPhone” and “Windows Mobile”, later, click on the “display” button :
總結(jié)
以上是生活随笔為你收集整理的android复选按钮,Android的复选框的详细开发案例分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 德国购物应用 OTTO 率先使用谷歌 A
- 下一篇: android mvc mvp 简书,浅