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 ...
随机推荐
- 迭代的模块itertools
itertools模块提供的全部是处理迭代功能的函数,他们的返回值不是list,而是迭代对象,只有在for循环的时候才会真正去计算. 使用迭代器的好处是在循环的时候才去取值,而直接返回值为list的结 ...
- LRU Cache的实现
代码如下: 来自为知笔记(Wiz)
- 在Python中怎么表达True
在Python中怎么表达True 为False的几种情况 0为False,其他所有数值皆为True 空串("")为False,其他所有字符串皆为True 空list([])为F ...
- Web Api 2 怎么支持 Session
Add protected void Application_PostAuthorizeRequest() { System.Web.HttpContext.Current.SetSessionSta ...
- js_sl 分享
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- DataGrid loadData loadFilter
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Cl ...
- esb异常20160322_1948
异常1. Failed to find entry point for component, the following resolvers tried but failed: [ 2016-03-2 ...
- ESP8266调试问题
1 串口连接电脑调试时,GPIO15必须接地,否则没响应 2发送退出透传指令“+++”时,必须取消勾选发送新行.发送别的指令时须勾选. 另外若所刷固件版本为[Vendor:www.ai-thinker ...
- usb库文件usb_desc.c分析
参考<圈圈教你玩USB> usb协议中使用的是小端结构,所以实际数据在传输时是低字节在先的. 设备描述符的实现: 已知每个设备都必须有且仅有一个设备描述符,它的结构在USB协议中有详细的定 ...
- centos下安装mysql不能启动
初学者犯了个错误:yum安装mysql的命令是:yum -y install mysql-server,而不是yum -y install mysql ----------------------以下 ...