ITPub博客

首页 > IT职业 > IT职场 > Android的复选框的详细开发案例分析

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

IT职场 作者:yifanwu 时间:2021-09-09 09:50:25 0 删除 编辑

在本教程中,我们将向您展示如何创建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 :

图片描述

2. If “IPhone” is checked :

图片描述

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

图片描述

原文链接:http://www.apkbus.com/blog-919651-76493.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2157/viewspace-2813192/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-24

  • 博文量
    84
  • 访问量
    12526