内容
代码
<?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"
tools:context=".CheckBoxActivity">
<TextView
android:id="@+id/check_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你会什么开发语言?"/>
<CheckBox
android:id="@+id/check_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c"
android:layout_below="@id/check_textview"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/check_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java"
android:layout_below="@id/check_1"
android:textSize="20sp"/>
</RelativeLayout>
public class CheckBoxActivity extends AppCompatActivity {
private CheckBox check_1;
private CheckBox check_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
check_1 = findViewById(R.id.check_1);
check_2 = findViewById(R.id.check_2);
check_1.setOnCheckedChangeListener((buttonView, isChecked) -> {//c复选按钮选择事件
Toast.makeText(CheckBoxActivity.this,isChecked ? "c被选中" : "c未被选中",
Toast.LENGTH_SHORT).show();
});
check_2.setOnCheckedChangeListener((buttonView, isChecked) -> {//java复选按钮选择事件
Toast.makeText(CheckBoxActivity.this,isChecked ? "java被选中" : "java未被选中",
Toast.LENGTH_SHORT).show();
});
}
}
效果