您的位置:首页 > 产品设计 > UI/UE

Android UI控件详解-CheckBox(多选框)

2014-04-06 20:55 501 查看
package com.example.duoxuan;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
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;

/**
*
* @author TXF
*
* 多选框
* 还有一种相对简单的方法来实现,这里就不再写了。
*
*/
public class MainActivity extends Activity {
private CheckBox mcb1, mcb2, mcb3, mcb4;
private Button mbtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcb1 = (CheckBox) findViewById(R.id.cb_0);
mcb2 = (CheckBox) findViewById(R.id.cb_1);
mcb3 = (CheckBox) findViewById(R.id.cb_2);
mcb4 = (CheckBox) findViewById(R.id.cb_3);
mbtn = (Button) findViewById(R.id.submit);
// 设置被选择监听器,这个监听器和RadioGroup的监听器是一样的
mcb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (mcb1.isChecked()) {
displayToast("你选择了" + mcb1.getText());
}
}
});
mcb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (mcb2.isChecked()) {
displayToast("你选择了" + mcb2.getText());
}
}
});
mcb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (mcb3.isChecked()) {
displayToast("你选择了" + mcb3.getText());
}
}
});
mcb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (mcb4.isChecked()) {
displayToast("你选择了" + mcb4.getText());
}
}
});

mbtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int num = 0;
if (mcb1.isChecked()) {
num++;
}
if (mcb2.isChecked()) {
num++;
}
if (mcb3.isChecked()) {
num++;
}
if (mcb4.isChecked()) {
num++;
}

displayToast("谢谢诶参与,您一共选择了" + num + "项");
}
});
}

private void displayToast(String str) {
Toast toast = Toast.makeText(this, str, 0);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

}

}

xml布局

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="你的爱好是什么"
android:textSize="20sp" />

<CheckBox
android:id="@+id/cb_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:checked="true"
android:text="听歌" />

<CheckBox
android:id="@+id/cb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_0"
android:text="跳舞" />

<CheckBox
android:id="@+id/cb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_1"
android:text="上网" />

<CheckBox
android:id="@+id/cb_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_2"
android:text="睡觉" />

<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/cb_3"
android:text="提交" />
效果图

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