CheckBox
- 常见属性
- 自定义样式
- 监听事件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/tt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你那天上班?"
android:textSize="30sp"
android:layout_marginBottom="20dp"/>
<CheckBox
android:id="@+id/Cb_1"
android:layout_below="@id/tt_1"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周一"/>
<CheckBox
android:id="@+id/Cb_2"
android:layout_below="@+id/Cb_1"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周二"/>
<CheckBox
android:id="@+id/Cb_3"
android:layout_below="@+id/Cb_2"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周三"/>
</RelativeLayout>
自己定义样式
复制到
图标太大 就去换小一点的图标
package com.example.myapplicationble;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class CheckBoxActivity extends AppCompatActivity {
private CheckBox mCb4,mCb5,mCb6;//声明
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
mCb4 = findViewById(R.id.Cb_4);//绑定
mCb5 = findViewById(R.id.Cb_5);//绑定
mCb6 = findViewById(R.id.Cb_6);//绑定
mCb4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//设置监听事件
Toast.makeText(CheckBoxActivity.this, b?"4选中":"4未选中", Toast.LENGTH_SHORT).show();
}
});
mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//设置监听事件
Toast.makeText(CheckBoxActivity.this, b?"5选中":"5未选中", Toast.LENGTH_SHORT).show();
}
});
mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//设置监听事件
Toast.makeText(CheckBoxActivity.this, b?"6选中":"6未选中", Toast.LENGTH_SHORT).show();
}
});
}
}