CheckBox 是一种多选按钮,用户可以在一组选项中选择多个。CheckBox也是Android中最常用的组件。
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text = "你希望能够做你的红颜知己的女人:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox android:text="杨贵妃" android:id="@+id/checkbox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox android:text="貂蝉" android:id="@+id/checkbox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox android:text="西施" android:id="@+id/checkbox3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox android:text="王昭君" android:id="@+id/checkbox4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btnGetCheckValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取选择的值"/>
</LinearLayout>
CheckBox 通过isChecked方法来确定是否选中。
CheckBox 支持OnClickListener 以及OnCheckedChangeListener:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
final CheckBox cb1 = (CheckBox)findViewById(R.id.checkbox1);
final CheckBox cb2 = (CheckBox)findViewById(R.id.checkbox2);
final CheckBox cb3 = (CheckBox)findViewById(R.id.checkbox3);
final CheckBox cb4 = (CheckBox)findViewById(R.id.checkbox4);
final CheckBox[] cbs= new CheckBox[]{cb1,cb2,cb3,cb4};
Button btnGetCheckValue = (Button)findViewById(R.id.btnGetCheckValue);
btnGetCheckValue.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String text = "你选择的红颜知己有:";
for(CheckBox cb : cbs){
if(cb.isChecked()){
text += cb.getText()+",";
}
}
setTitle(text);
}
});
for(final CheckBox cb: cbs){
cb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println(cb.getText());
}
});
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
setTitle("你选了" +cb.getText());
Toast.makeText(getApplicationContext(), "you selected "+buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
分享到:
相关推荐
android.widget 类 CheckBox java.lang.Object 继承者 android.view.View 继承者 android.widget.TextView 继承者 android.widget.Button 继承者 android.widget.CompoundButton 继承者 android.widget.CheckBox ...
为大家分享一套Android widget窗口小部件用法实例集源码,在这个例子中,将涉及到一些大家非常熟悉而且非常有用的窗体UI组件的用法,比如Button按钮、textView、Editview编辑框、Checkbox选择框、RadioGroup单选框、...
复选框(CheckBox)的使用.doc、 进度条(ProgressBar)的使用.doc、 可编辑文本框(EditText)的使用.doc、 列表框(ListView)的使用.doc、 日期选择器(DatePicker)的使用.doc、 时间选择器(TimePicker)的使用.doc、 ...
android设计官方文档!~Android官方API文档完整版、中文版帮助文档 ... 继承者 android.widget.CheckBox 所有已实现的接口: Drawable.Callback, KeyEvent.Callback, ViewTreeObserver.OnPreDrawListener, Checkable
本文实例为大家分享了CheckBox全选、反选、单选的具体代码,供大家参考,具体内容如下 MainActiivity package com.bwie.day06; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; ...
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;...import android.widget.CheckBox; import android.widget.TextView;
本文实例讲述了Android开发中CheckBox的简单用法。分享给大家供大家参考,具体如下: CheckBox是一种在界面开发中比较常见的控件,Android中UI开发也有CheckBox,...import android.widget.CheckBox; import android.wi
Android 中CheckBox的isChecked的使用实例详解 范例说明 所有的网络服务在User使用之前,都需要签署同意条款,在手机应用程序、手机游戏的设计经验中,常看见CheckBox在同意条款情境的运用,其选取的状态有两种即...
AppWidget 显示一些高级android appwidget属性,例如listview,复选框(使用imagebutton实现),刷新数据运行时以及使用shared_prefs保持状态。 例如:1:listview + checkbox Appwidget支持listview,但不支持...
android widget TextView 直接子类: Button CheckedTextView Chronometer DigitalClock EditText 间接子类: AutoCompleteTextView CheckBox CompoundButton ExtractEditText MultiAutoCompleteTextView ...
4.2.6 多项选择(CheckBox) 4.2.7 下拉列表(Spinner) 4.2.8 自动提示(AutoComplete.TextⅥew) 4.2.9 日期和时间(DatePicker、TimePicker) 4.2.10 按钮(Button) 4.2.1l 菜单(Menu) 4.2.12 对话框(Dialog) 4.2.13 图片...
import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.Toast; private EditText et1; private EditText et2; private CheckBox cb; private...
能够搜索,两套API,Android官方API文档完整版、中文版帮助文档 android.widget 类 CheckBox java.lang.Object 继承者 android.view.View
android用户界面之Widget教程实例汇总 android用户界面之TabHost教程实例汇总 android用户界面之Gallery教程实例汇总 android用户界面之按钮(Button)教程实例汇 android用户界面之ProgressBar教程实例汇总 android...
android5.0后,google对checkbox,switch,...那我们不用setLayoutResource/setWidgetLayoutResource接口,改用android:widgetLayout属性,不就没有这个问题啦。这里以RadioPreference为例,写个简单的demo,仅供参考
首先,写好自己需要的布局的xml文件 当然复选框中的文字可以自己选择 复选框的处理事件我写了很多种 ... import androidx.appcompat....import android.widget.CheckBox; import android.widget.CompoundButton; im
4.2.6 多项选择(CheckBox) 4.2.7 下拉列表(Spinner) 4.2.8 自动提示(AutoComplete.TextⅥew) 4.2.9 日期和时间(DatePicker、TimePicker) 4.2.10 按钮(Button) 4.2.1l 菜单(Menu) 4.2.12 对话框(Dialog...
使用CheckBox需要导入android.widget.CheckBox和android.widget.CompoundButton。 CheckBox的创建 使用xml布局创建 使用xml布局创建CheckBox的核心在于布局文件和findviewById()方法。 activity_main.xml ...
4.2.6 多项选择(CheckBox) 4.2.7 下拉列表(Spinner) 4.2.8 自动提示(AutoComplete.TextⅥew) 4.2.9 日期和时间(DatePicker、TimePicker) 4.2.10 按钮(Button) 4.2.1l 菜单(Menu) 4.2.12 对话框(Dialog) 4.2.13 图片...