Android Checkbox Example
1. Custom String
打开 “res/values/strings.xml” 文件,
File : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">CheckBoxDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</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>
2. CheckBox
打开 “res/layout/activity_main.xml” 文件,
File : res/layout/activity_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>
注: android:checked="true" 表示默认选中。本例子中,chkAndroid默认选中
3. Code Code
package com.jiangge.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 MainActivity extends Activity { private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); addListenerOnChkIos();
addListenerOnButton();
} private void addListenerOnChkIos() {
chkIos = (CheckBox) findViewById(R.id.chkIos); chkIos.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// chkIos 是否被选中
if (((CheckBox)v).isChecked()) {
Toast.makeText(getApplicationContext(), "Bro try android,哈哈",Toast.LENGTH_LONG).show();
}
}
});
} private 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() { @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(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}
}); } }
注:
if (((CheckBox)v).isChecked()) 向下转型。
StringBuffer result = new StringBuffer();
result .append().append()
4、运行结果:
一启动时的运行结果:
选中 IPhone (和Android)的运行结果:
选中IPone 和 Winows Mobile,然后点击 Button 按钮的运行结果:
Android Checkbox Example的更多相关文章
- xamarin android checkbox自定义样式
xamarin android checkbox自定义样式 在drawable文件在新建checkbox_bg.xml文件 <?xml version="1.0" encod ...
- android CheckBox控件的定义及事件监听
http://www.beijibear.com/index.php?aid=336 android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果 ...
- android CheckBox的运用
CheckBox定义一个同意协议的按钮,只要同意button才可以点击 XML代码 <CheckBox android:id="@+id/checkbox1" android ...
- Android checkbox 自定义点击效果
安卓默认的效果 自定义后的效果 前面的图片当然可以自己修改. 实现这个效果的步骤如下 1.建立 一个selector 的xml <?xml ver ...
- Android checkBox
checkBox 状态:选中(true),未选中(false) 属性: checked="true/false"; private Chec ...
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...
- android CheckBox RadioButton 照片和文字的间距问题
利用自身的定义CheckBox 要么RadioButton时间.定义自己的图标和文字在不同的手机显示不同的音高.有时不太好控制,下面是我自己的定义CheckBox: 在Layout在下面xml: &l ...
- android checkbox 未选中状态 已选中状态 替换成自己的图片
效果图: 未选中状态: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- android CheckBox与监听
<CheckBox android:id="@+id/cb1" android:layout_width="fill_parent" android ...
随机推荐
- cdh5.4、cm5.4 安装详细步骤
安装准备: 1.centos6.5 64位 虚拟机,内存分配4G.硬盘位20G 2.cm5.4 cdh5.4 包 安装步骤 一.centos安装完后,进行系统配置 1.关闭防火墙 service ip ...
- 【LeetCode】198 - House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- uoj #58. 【WC2013】糖果公园(树上莫队算法+修改操作)
[题目链接] http://uoj.ac/problem/58 [题意] 有一棵树,结点有自己的颜色,若干询问:u,v路径上的获益,并提供修改颜色的操作. 其中获益定义为Vc*W1+Vc*W2+…+V ...
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
跨域攻击---自然来路页面和目标页面不在同一个域下,所以直接判断来路域和当前自己的域就可以了. 可以广泛应用于表单提交,ajax调用或者某些不想让用户直接输入网址看到的页面 [csharp] view ...
- jbpm4.4+ssh配置(有些使用经验很好)
http://www.cnblogs.com/cmzcheng/archive/2011/11/20/2255806.html ———————————————————————————————————— ...
- ESP8266 TCP传输AT指令顺序
); //复位 ret = ESP8266_Cmd ( );//测试AT启动 ret = ESP8266_Cmd ( );//选择WIFI应用模式softAP+station //ret = ESP8 ...
- JS制作的简单的三级及联
前台: <form id="form1" runat="server"> <div> 省 <select id="Pro ...
- android ListView中button点击事件盖掉onItemClick解决办法
ListView 1.在android应用当中,很多时候都要用到listView,但如果ListView当中添加Button后,ListView 自己的 public void onItemClick ...
- 新浪SAE数据库信息wordpress设置(用户&密码&主地址)
新浪SAE数据库信息wordpress设置(用户&密码&主地址) 此账号仅能在SAE平台上使用,不能从外部连接我们建议开发者使用SaeMysql操作数据库如果您想自己实现数据库相关操作 ...
- 标准Dispose实现 (转)
需要明确一下C#程序(或者说.NET)中的资源.简单的说来,C#中的每一个类型都代表一种资源,而资源又分为两类: 托管资源:由CLR管理分配和释放的资源,即由CLR里new出来的对象: 非托管资源 ...