Android CheckBox example

本文提供了一个使用 Android CheckBox 控件的示例应用,展示了如何创建并监听 CheckBox 的状态改变,并通过按钮显示选择结果。代码中包含了 strings.xml 和 main.xml 布局文件配置,以及 CheckBoxDemoActivity 类实现。

strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="hello">Hello World, CheckBoxDemoActivity!</string>
    <string name="app_name">CheckBoxDemo</string>
<string name="chk_ios">IPhone</string>
    <string name="chk_android">Android</string>
    <string name="chk_windows">Windows Mobile</string>
    <string name="btn_display">Display</string>
</resources>


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <CheckBox
        android:id="@+id/chkIos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_ios" />
 
    <CheckBox
        android:id="@+id/chkAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_android"
        android:checked="true" />
 
    <CheckBox
        android:id="@+id/chkWindows"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_windows" />
 
    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />
 
</LinearLayout>


CheckBoxActivity.java


package com.cjp.checkboxdemo;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;


public class CheckBoxDemoActivity extends Activity {
private CheckBox chkIos, chkAndroid, chkWindows;
 private Button btnDisplay;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
addListenerOnChkIos();
addListenerOnButton();
 }
 
 public void addListenerOnChkIos() {
 
chkIos = (CheckBox) findViewById(R.id.chkIos);
 
chkIos.setOnClickListener(new OnClickListener() {
 
 @Override
 public void onClick(View v) {
               //is chkIos checked?
if (((CheckBox) v).isChecked()) {
Toast.makeText(CheckBoxDemoActivity.this,
  "Bro, try Android :)", Toast.LENGTH_LONG).show();
}
 
 }
});
 
 }
 
 public void 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(new OnClickListener() {
 
         //Run when button is clicked
 @Override
 public void onClick(View v) {
 
StringBuffer result = new StringBuffer();
result.append("IPhone check : ").append(chkIos.isChecked());
result.append("\nAndroid check : ").append(chkAndroid.isChecked());
result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
 
Toast.makeText(CheckBoxDemoActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
 
 }
});
 
 }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值