android控件06---CheckBox
CheckBox这个控件和RadioButton差不多,差别只在于前者是多选框,后者通常作为单选框。
下面还是通过一个例子来介绍这个控件:
XML中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="下面哪些是android里的控件?"/>
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:text="Button"/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:layout_below="@+id/box1"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:layout_below="@+id/box2"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android"
android:layout_below="@+id/box3"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/box4"
android:layout_centerHorizontal="true"
android:text="提交"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="下面哪些是android里的控件?"/>
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:text="Button"/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:layout_below="@+id/box1"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:layout_below="@+id/box2"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android"
android:layout_below="@+id/box3"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/box4"
android:layout_centerHorizontal="true"
android:text="提交"/>
</RelativeLayout>
Activity代码如下:
package xy.kj;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView text;
private CheckBox box1;
private CheckBox box2;
private CheckBox box3;
private CheckBox box4;
private Button button;
private String str1 = null,str2= null,str3 = null,str4 = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.text);
box1 = (CheckBox)findViewById(R.id.box1);
box2 = (CheckBox)findViewById(R.id.box2);
box3 = (CheckBox)findViewById(R.id.box3);
box4 = (CheckBox)findViewById(R.id.box4);
button = (Button)findViewById(R.id.button);
//对每个选项设置监听事件
box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box1.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+box1.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box1.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
box2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box2.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+box2.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box2.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
box3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box3.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+box3.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box3.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
box4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box4.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+ box4.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box4.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = null;
if (box1.isChecked() && box2.isChecked() && box3.isChecked()){
str = "回答正确!";
}else{
str = "回答错误!";
}
Toast toast = Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
});
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView text;
private CheckBox box1;
private CheckBox box2;
private CheckBox box3;
private CheckBox box4;
private Button button;
private String str1 = null,str2= null,str3 = null,str4 = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.text);
box1 = (CheckBox)findViewById(R.id.box1);
box2 = (CheckBox)findViewById(R.id.box2);
box3 = (CheckBox)findViewById(R.id.box3);
box4 = (CheckBox)findViewById(R.id.box4);
button = (Button)findViewById(R.id.button);
//对每个选项设置监听事件
box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box1.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+box1.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box1.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
box2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box2.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+box2.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box2.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
box3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box3.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+box3.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box3.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
box4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (box4.isChecked()){
Toast toast = Toast.makeText(MainActivity.this,"你选择了:"+ box4.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this,"你取消了选择:"+box4.getText(),Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = null;
if (box1.isChecked() && box2.isChecked() && box3.isChecked()){
str = "回答正确!";
}else{
str = "回答错误!";
}
Toast toast = Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,500);
toast.show();
}
});
}
}
运行效果图如下:
1.选取按钮时会弹出消息提示
2.取消选取是也会出现消息提示
3.选择错误
4.选择正确