布局文件中写入
<CheckBox
android:id="@+id/mcBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="是"
android:textSize="25sp"/>
Java文件中写入,使MainActivity知道此选项是否被选中
public class MainActivity extends Activity{
private CheckBox checkBox;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
init();
//设置监听器
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {
//通过判断ischecked来判断控件是否被选中
if (ischecked){
//如果被下选中,获取其文本内容并打印
String text = checkBox.getText().toString();
Log.i("tag",text);
}
else if (ischecked==false){
Log.i("tag","否");
}
}
});
}
public void init(){
checkBox = findViewById(R.id.mcBox);
}
}
最后效果: