安卓复选框(Checkbox)
16lz
2021-01-24
●设置复选框的Check状态的时候,调用setChecked()方法
●追加Android复选框被选择时处理的时候,
1.调用setOnCheckedChangeListener()方法,并把CompoundButton.OnCheckedChangeListener实例作为参数传入
2.在CompoundButton.OnCheckedChangeListener的onCheckedChanged()方法里,取得被选中Android复选框的实例
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:background="@drawable/background2" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱好" /> <CheckBox android:id="@+id/box1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球" > </CheckBox> <CheckBox android:id="@+id/box2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球" > </CheckBox> <CheckBox android:id="@+id/box3" android:layout_width="wrap_content" android:layout_height="wrap_content" t="" android:text="排球" > </CheckBox> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择" /></LinearLayout>
java代码:
package com.xzy.demo.checkbox;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.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast;public class CheckedBoxActivity extends Activity {private CheckBox box1;private CheckBox box2;private CheckBox box3;private Button button;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);box1 = (CheckBox) findViewById(R.id.box1);box2 = (CheckBox) findViewById(R.id.box2);box3 = (CheckBox) findViewById(R.id.box3);button = (Button) findViewById(R.id.button1);// 复选框被选中的事件OnCheckedChangeListener listener = new OnCheckedChangeListener() {// 表示被选中的复选框和是否被选中public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {Toast.makeText(CheckedBoxActivity.this,buttonView.getText(), 3000).show();}}};// 为box按钮组绑定监听事件box1.setOnCheckedChangeListener(listener);box2.setOnCheckedChangeListener(listener);box3.setOnCheckedChangeListener(listener);// 按钮点击事件button.setOnClickListener(new OnClickListener() {public void onClick(View v) {String text = "";if (box1.isChecked()) {text += box1.getText();}if (box2.isChecked()) {text += box2.getText();}if (box3.isChecked()) {text += box3.getText();}Toast.makeText(CheckedBoxActivity.this, text, 3000).show();}});}}
截图:
更多相关文章
- Android定时器实现的几种方法
- android BaseAdapter优化
- 可下拉的PinnedHeaderExpandableListView的实现
- Android:在Eclipse下开发android应用产生的问题及解决方法
- android判断软件是否第一次运行的方法
- Android(安卓)AlertDialog 无法弹出输入法
- [2].FFMpeg入门之 - Android使用FFMpeg命令
- android dialog屏蔽back返回键的解决方法
- Android简单明了的使用属性动画ObjectAnimator 旋转 平移 渐变