Android的核取方塊的詳細開發案例分析
在本教程中,我們將向您展示如何建立XML檔案中的3個核取方塊,並論證了聽者的使用檢查–選中或取消選中核取方塊的狀態。
P.S這個專案是在Eclipse 3.7開發,並與Android 2.3.3測試。
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。核取方塊
Open “res/layout/ main.xml” file, add 3 “核取方塊” and a button, inside the 線性佈局.
檔案:res/layout/ main.xml
<LinearLayout xmlns: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>
使核取方塊預設被選中
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。setlistener(新 listener() { @Override 公共 無效 (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。setlistener(新 listener() { clicked button is when /執行 @Override 公共 無效 (View v) { StringBuffer result = 新 StringBuffer() result。追加(“iPhone檢查:”)。追加(chkIos。把關()) result。追加(“nandroid檢查:”)。追加(chkAndroid。把關()) result。追加(“移動nwindows檢查:”)。追加(chkWindows。把關()) 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 :
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2157/viewspace-2813192/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [開發教程]第18講:Bootstrap核取方塊boot
- 核取方塊和切換按鈕的7個使用案例
- DevExpress XtraTreeList的核取方塊 禁用devExpress
- js獲取checkbox核取方塊checked屬性值為true核取方塊JS
- JavaScript核取方塊全選和全不選詳解JavaScript
- CSS模擬美化checkbox核取方塊詳解CSS
- js 獲取被選中核取方塊的值JS
- jQuery獲取選中的checkbox核取方塊的值jQuery
- HTML input checkbox 核取方塊HTML
- HTML input checkbox核取方塊HTML
- ElementUi Table核取方塊回顯UI
- CSS 美化checkbox核取方塊CSS
- jQuery checkbox核取方塊操作jQuery
- jquery table下的核取方塊選中、取消jQuery
- JavaScript 獲取選中checkbox核取方塊的值JavaScript
- jquery獲取選中checkbox核取方塊的值jQuery
- js獲取選中checkbox核取方塊的值JS
- javascript獲取選中checkbox核取方塊的值JavaScript
- Element原始碼分析系列6-Checkbox(核取方塊)原始碼
- jquery獲取沒有選中的checkbox核取方塊jQuery
- JavaScript獲取選中checkbox核取方塊的選中值JavaScript
- 直播app開發,核取方塊選中所在的行,右側按鈕禁掉APP
- 一個數值儲存核取方塊的值
- 使用jQuery做核取方塊的全選與取消jQuery
- JavaScript刪除核取方塊選中的表格行JavaScript
- 單選按鈕和核取方塊
- javascript實現核取方塊全選和取消程式碼分析JavaScript
- jQuery獲取選中的核取方塊或者單按鈕的值jQuery
- UX設計之——核取方塊和開關按鈕UX
- 使用jquery獲取被選中checkbox核取方塊的值jQuery
- jQuery獲取選中核取方塊的值程式碼例項jQuery
- jQuery Validate限定核取方塊選中的數目jQuery
- jQuery核取方塊checkbox的全選和反選jQuery
- js實現checkbox核取方塊的反選效果JS
- layui獲取頁面checkbox核取方塊值UI
- JavaScript獲取選中checkbox核取方塊值JavaScript
- jQuery獲取所有的checkbox核取方塊元素jQuery
- PyQT5 實現下拉核取方塊QT