本帖最后由 liu 于 2018-12-5 11:45 编辑
CheckBox
基本用法:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示文本" />
</LinearLayout>
举个例子:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<!-- android:checked="true"设置默认选中 -->
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="开封" />
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="郑州" />
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京" />
</LinearLayout>
效果如下图:
CheckBox的监听事件:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private CheckBox mCheckBox1;
private CheckBox mCheckBox2;
private CheckBox mCheckBox3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCheckBox1 = (CheckBox) findViewById(R.id.checkbox1);
mCheckBox2 = (CheckBox) findViewById(R.id.checkbox2);
mCheckBox3 = (CheckBox) findViewById(R.id.checkbox3);
//通过设置checkbox的监听事件,判断checkbox是否被选中
mCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "选中了开封", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "取消选中开封", Toast.LENGTH_SHORT).show();
}
}
});
mCheckBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "选中了郑州", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "取消选中郑州", Toast.LENGTH_SHORT).show();
}
}
});
mCheckBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "选中了北京", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "取消选中北京", Toast.LENGTH_SHORT).show();
}
}
});
}
}
效果如下图:
|