android复选按钮,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=“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>

使复选框默认被选中

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 :

AAffA0nNPuCLAAAAAElFTkSuQmCC

2. If “IPhone” is checked :

AAffA0nNPuCLAAAAAElFTkSuQmCC

3. Checked “IPhone” and “Windows Mobile”, later, click on the “display” button :

AAffA0nNPuCLAAAAAElFTkSuQmCC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值