Thursday, March 12, 2020

Android Checkbox Example

The CheckBox extension from CompoundButton creates a Button type that allows to display checkboxes, show checked or unchecked status information, code to customize CheckBox

CompoundButton

CompoundButton is an abstract base class extending from TextView (Button), from this class it is extended to build View: CheckBox, RadioButton, Switch, ToggleButton

The CheckBox, RadioButton, Switch, ToggleButton extension classes save properties and behaves similar to TextView, Button but have two checked and unchecked states.
Some properties, common methods for CheckBox, RadioButton, Switch, ToggleButton
In XML
android: checked sets the checked and unchecked status to "true" or "false"
android: button to assign Drawable to View (draw status for CheckBox, RadioButton ...)
android: buttonTint to assign Tint color
In Java code
isChecked () to check if the status is checked (true) or unchecked (false)
setOnCheckedChangeListener catches the event when the (checked / unchecked) state converts
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
         
        }
    });
CheckBox Android Example
Note when using the CheckBox it has the same properties as TextView, Button
in XML
<CheckBox
    android:id="@+id/checkbox_id"
    android:text="Java - Android"
    android:checked="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Code java
final CheckBox checkBox = findViewById(R.id.checkbox_id);
if (checkBox.isChecked())
{
    //Checked
}
else
{
    //Unchecked
}
boolean toi_chon = true;
checkBox.setChecked(toi_chon);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
     
        Toast.makeText(
                compoundButton.getContext(),
                compoundButton.getText()+"|"+b,
                Toast.LENGTH_SHORT).show();
    }
});
checkBox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(
                view.getContext(), "Click!",
                Toast.LENGTH_SHORT).show();
    }
});
Example checkbox in Android
layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="5dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="Loại biến nào có thể dùng cho tên một thành phố?"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/int_id"
        android:text="int"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/double_id"
        android:text="double"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/string_id"
        android:text="String"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/all"
        android:text="Tất cả các phương án"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/test"
        android:text="Kết quả"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/hint"
        android:text="Gợi ý"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
    CheckBox int_id, double_id, string_id, all;
    Button test, hint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    CompoundButton.OnCheckedChangeListener m_listener
        = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (compoundButton == all)
            {
                detachListener();
                int_id.setEnabled(!b);
                double_id.setEnabled(!b);
                string_id.setEnabled(!b);
                int_id.setChecked(b);
                double_id.setChecked(b);
                string_id.setChecked(b);
                attachListener();
            }
            else {
                Toast.makeText(compoundButton.getContext(),
                        compoundButton.getText() + " | "
                        + compoundButton.isChecked(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    };
    void attachListener()
    {
        int_id.setOnCheckedChangeListener(m_listener);
        double_id.setOnCheckedChangeListener(m_listener);
        string_id.setOnCheckedChangeListener(m_listener);
        all.setOnCheckedChangeListener(m_listener);
    }
    void detachListener()
    {
        int_id.setOnCheckedChangeListener(null);
        double_id.setOnCheckedChangeListener(null);
        string_id.setOnCheckedChangeListener(null);
        all.setOnCheckedChangeListener(null);
    }

    void init() {
        int_id    = findViewById(R.id.int_id);
        double_id = findViewById(R.id.double_id);
        string_id = findViewById(R.id.string_id);
        all       = findViewById(R.id.all);
        attachListener();

        test = findViewById(R.id.test);
        hint  = findViewById(R.id.hint);
        hint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                detachListener();
                int_id.setChecked(false);
                double_id.setChecked(false);
                all.setChecked(false);
                attachListener();
                string_id.setChecked(true);
            }
        });
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mgs = "";
                if (!int_id.isChecked() &&
                        !double_id.isChecked() &&
                        string_id.isChecked())
                    mgs = "Đúng, chúc mừng";
                else
                    mgs = "Sai rồi";
                Toast.makeText(view.getContext(),
                        mgs,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}
Android Checkbox Example

No comments:

Post a Comment