Android的复选框的详细开发案例分析

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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文件:

<?xml version="1.0" encoding="utf-8"?>
<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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout rel="nofollow noreferrer" target="_blank">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();

  }
});

}
}

4. Demo

Run the application.

1. Result :

Android的复选框的详细开发案例分析

2. If “IPhone” is checked :

Android的复选框的详细开发案例分析

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

Android的复选框的详细开发案例分析

原文地址:http://www.apkbus.com/blog-91...

脚本宝典总结

以上是脚本宝典为你收集整理的Android的复选框的详细开发案例分析全部内容,希望文章能够帮你解决Android的复选框的详细开发案例分析所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。