checkbox(复选框),用于多个选择选中或者多个选择不选等情况下使用,例如你喜欢吃的水果可有多种,或者都没有,也可以只有一种。checkbox控件其实就是一个button并附带一个可以检测目前是否被选中的状态变量isChecked,如果被选中isChecked为true,否则为flase.
例如如下的效果图:
xml布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/id_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你喜欢的水果的是?"/>
<CheckBox
android:id="@+id/id_apple"
android:layout_below="@id/id_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果"/>
<CheckBox
android:id="@+id/id_banana"
android:layout_below="@id/id_apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="香蕉"/>
<CheckBox
android:id="@+id/id_orange"
android:layout_below="@id/id_banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="橙子"/>
</RelativeLayout>
java代码逻辑:
package com.gentle.checkbox;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView txShow;
private String selectTotal="";
private String appleSelected ="";
private String bananaSelected ="";
private String orangeSelected ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox apple = findViewById(R.id.id_apple);
apple.setOnCheckedChangeListener(checkBoxListenr);
CheckBox banana = findViewById(R.id.id_banana);
banana.setOnCheckedChangeListener(checkBoxListenr);
CheckBox orange = findViewById(R.id.id_orange);
orange.setOnCheckedChangeListener(checkBoxListenr);
}
private CompoundButton.OnCheckedChangeListener checkBoxListenr = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.getId() == R.id.id_apple){
appleSelected = isChecked?"苹果":"";
}else if(buttonView.getId() == R.id.id_banana){
bananaSelected = isChecked?"香蕉":"";
}else if(buttonView.getId() == R.id.id_orange){
orangeSelected = isChecked?"橙子":"";
}else
Log.d("--->test","check id is invalid");
selectTotal = appleSelected + bananaSelected + orangeSelected;
Toast.makeText(MainActivity.this,selectTotal,Toast.LENGTH_SHORT).show();
}
};
}