CheckBox控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你會哪些移動開發"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginBottom="10dp"
/>
<CheckBox
android:id="@+id/cb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:textSize="20sp"
android:layout_below="@id/title"
/>
<CheckBox
android:id="@+id/cb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IOS"
android:textSize="20sp"
android:layout_below="@id/cb_1"
/>
<CheckBox
android:id="@+id/cb_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H5"
android:textSize="20sp"
android:layout_below="@id/cb_2"
/>
<LinearLayout
android:layout_width="291dp"
android:layout_height="wrap_content"
android:layout_below="@id/cb_3"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的興趣"
android:textColor="#000"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/bg_checkbox"
android:text="編程"
android:textSize="20sp" />
<CheckBox
android:id="@+id/cb_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/bg_checkbox"
android:text="做飯"
android:textSize="20sp" />
</LinearLayout>>
</RelativeLayout>
drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/right"/>
<item android:state_checked="false" android:drawable="@drawable/wrong" />
</selector>
CheckBoxActivity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class CheckBoxActivity extends AppCompatActivity {
private CheckBox mBc4,mBc5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
mBc4=findViewById(R.id.cb_4);
mBc5=findViewById(R.id.cb_5);
mBc4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this,isChecked?"選中":"未選中",Toast.LENGTH_SHORT.show());
}
});
mBc5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this,isChecked?"選中":"未選中",Toast.LENGTH_SHORT.show());
}
});
}
}
用到的圖片
效果如圖