您的位置:首页 > 移动开发 > Android开发

Android控件之CheckBox多选框

2015-10-27 19:00 489 查看
Android中的CheckBox控件既可以通过Button按钮来监听其选中状态,它也有自己的事件处理方法,通过一个小例子来看具体代码

如下图:





1.0 activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
tools:context="com.lgl.checkbox.MainActivity" >

<CheckBox
android:id="@+id/cb01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/huawei" />
<CheckBox
android:id="@+id/cb02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/iphone" />
<CheckBox
android:id="@+id/cb03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xiaomi" />

<Button
android:id="@+id/btn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/click" />

</LinearLayout>


2.0 MainActivity:

package com.lgl.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 MainActivity extends Activity {

private CheckBox ch01;
private CheckBox ch02;
private CheckBox ch03;
private Button btn01;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

/**
* 获取控件
*/
ch01 = (CheckBox) findViewById(R.id.cb01);
ch02 = (CheckBox) findViewById(R.id.cb02);
ch03 = (CheckBox) findViewById(R.id.cb03);
btn01 = (Button) findViewById(R.id.btn01);

/**
* 为三个CheckBox分别绑定监听
*/
ch01.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
String text = ch01.getText().toString();
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
ch02.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
String text = ch02.getText().toString();
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
ch03.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
String text = ch03.getText().toString();
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});

/**
* 为Button绑定监听
*/
btn01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String text = "您选择了:";
if(ch01.isChecked()) {
text += ch01.getText().toString();
}
if(ch02.isChecked()) {
text += ch02.getText().toString();
}
if(ch03.isChecked()) {
text += ch03.getText().toString();
}
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: