android 组件CheckBox实例

    本例子是对checkbox组件的小练习。用4个checkbox供选择今天吃什么食物。通过button点击确定后弹出提示对话框,提示选中的食物。

    主要应用了isChecked()方法。如果被勾选,则加入字符串中。

布局文件:checkbox_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="6dp" >


    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:text="今天要吃的食物有:" />


    <CheckBox
        android:id="@+id/cb_red_row"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="红烧肉" />


    <CheckBox
        android:id="@+id/cb_noodles"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拉面" />


    <CheckBox
        android:id="@+id/cb_checken"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="鸡排饭" />


    <CheckBox
        android:id="@+id/cb_cai_jiao_eggs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="菜椒蒸鸡蛋" />


    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="submit"
        android:text="提交" />


</LinearLayout>


AtyCheckBox.java文件:

package com.fxj.composit;


import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;


import com.fxj.compractice.R;


public class AtyCheckBox extends Activity {
private CheckBox redRow, noodles, checken, caiJiaoEggs;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox_layout);
// 实例化控件
caiJiaoEggs = (CheckBox) findViewById(R.id.cb_cai_jiao_eggs);
redRow = (CheckBox) findViewById(R.id.cb_red_row);
noodles = (CheckBox) findViewById(R.id.cb_noodles);
checken = (CheckBox) findViewById(R.id.cb_checken);
}


// Button监听事件
public void submit(View view) {
String str = "今天吃的食物有:\n";
if (redRow.isChecked()) {
str += "红烧肉\n";
}
if (noodles.isChecked()) {
str += "拉面\n";
}
if (checken.isChecked()) {
str += "鸡排饭\n";
}
if (caiJiaoEggs.isChecked()) {
str += "菜椒蒸鸡蛋\n";
}
// 弹出提示对话框
new AlertDialog.Builder(this).setTitle("结果").setMessage(str)
.setPositiveButton("确定", null).show();
}
}

运行效果:





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子林Android

感谢老板,老板大气!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值