CheckBox
自定义 CheckBox
主页面
package com.example.testjava1;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class CheckBoxActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
MyCheckView checkTextView = findViewById(R.id.check);
checkTextView.setOnClickListener(v->{
checkTextView.setSTATUS(!checkTextView.getSTATUS());
});
}
}
主页面的 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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".CheckBoxActivity">
<com.example.testjava1.MyCheckView
android:layout_width="wrap_content"
android:text="请勾选"
android:id="@+id/check"
android:textSize="30dp"
android:layout_height="wrap_content"/>
</LinearLayout>
自定义的 checkbox 页面
package com.example.testjava1;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
public class MyCheckView extends androidx.appcompat.widget.AppCompatTextView {
private static final String TAG = "MyCheckView";
private final Drawable unCheck;
private final Drawable check;
private Boolean STATUS = false;
public MyCheckView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
check = ContextCompat.getDrawable(context, R.mipmap.check);
unCheck = ContextCompat.getDrawable(context, R.mipmap.uncheck);
setCompoundDrawablesWithIntrinsicBounds(unCheck, null, null, null);
}
public void setSTATUS(Boolean STATUS) {
this.STATUS = STATUS;
Log.i(TAG, "setSTATUS: 设置状态");
if (STATUS) {
setCompoundDrawablesWithIntrinsicBounds(check, null, null, null);
return;
}
setCompoundDrawablesWithIntrinsicBounds(unCheck, null, null, null);
}
public Boolean getSTATUS() {
return STATUS;
}
}
官方的 checkbox 页面的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MyCheckboxView">
<CheckBox
android:id="@+id/my_checkbox_view"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="勾选,表示同意用户协议" />
</LinearLayout>
官方的 checkbox 页面 Click 的写法
package com.example.testjava1;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MyCheckboxView extends AppCompatActivity {
private static final String TAG = "MyCheckboxView";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_main);
CheckBox checkBox = findViewById(R.id.my_checkbox_view);
checkBox.setOnClickListener(v -> {
boolean isChecked = checkBox.isChecked();
Toast toast = Toast.makeText(MyCheckboxView.this, "当前状态:" + isChecked, Toast.LENGTH_SHORT);
Log.d("CheckBox", "当前状态:" + isChecked);
toast.show();
if (isChecked) {
checkBox.setChecked(true);
return;
}
checkBox.setChecked(false);
});
}
}
官方的 checkbox 页面 监听 的写法1
package com.example.testjava1;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MyCheckboxView extends AppCompatActivity {
private static final String TAG = "MyCheckboxView";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_main);
CheckBox checkBox = findViewById(R.id.my_checkbox_view);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(@NonNull CompoundButton buttonView, boolean isChecked) {
CheckBox btnView1 = (CheckBox) buttonView;
btnView1.isChecked();
Toast toast = Toast.makeText(MyCheckboxView.this, "当前状态:" + isChecked, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
官方的 checkbox 页面 监听 的写法2
package com.example.testjava1;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MyCheckboxView extends AppCompatActivity {
private static final String TAG = "MyCheckboxView";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_main);
CheckBox checkBox = findViewById(R.id.my_checkbox_view);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(@NonNull CompoundButton buttonView, boolean isChecked) {
CheckBox btnView1 = (CheckBox) buttonView;
boolean isChecked1 = btnView1.isChecked();
Toast toast = Toast.makeText(MyCheckboxView.this, "当前状态:" + isChecked1, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}