当前位置:网站首页>Android Basics - check box

Android Basics - check box

2020-11-08 12:06:00 ZHAO_JH

CheckBox( Check box )

You can select multiple options at the same time , As for getting the selected value , There are also two ways : 1. For each CheckBox Add event :setOnCheckedChangeListener 2. Get a button , After clicking , For each checkbox Judge :isChecked();

Example layout activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <CheckBox
        android:id="@+id/cbSleep"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" sleep " />
    <CheckBox
        android:id="@+id/cbSing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Sing a song " />
    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Output "
        android:onClick="show" />
</LinearLayout>

MainActivity.java

package com.example.myapplication;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener {
    //  establish map Dictionaries are used to store selected content 
    private Map map_dict = new HashMap();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //  Get controls 
        CheckBox cbSing = (CheckBox) findViewById(R.id.cbSing);
        CheckBox cbSleep = (CheckBox) findViewById(R.id.cbSleep);
        //  The binding event 
        cbSing.setOnCheckedChangeListener(this);
        cbSleep.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton checkBox, boolean checked) {

        //  Judge whether to select , Select to add data to map In the dictionary , If there is none, it will be removed by default mao All the data in the dictionary 
        switch (checkBox.getId()) {
            case R.id.cbSing:
                if (checked) {
                    map_dict.put("sing", " Sing a song ");
                }else {
                    map_dict.remove("sing");
                }
                break;
            case R.id.cbSleep:
                if (checked) {
                    map_dict.put("sleep", " sleep ");
                }else {
                    map_dict.remove("sleep");
                }
                break;
            default:
                break;
        }
    }

    public void show(View v) {
        //  Judge map Whether there is data in the dictionary 
        if(map_dict.isEmpty()){
            Toast.makeText(MainActivity.this, " No data selected ", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), " Your chosen hobby is :" + map_dict, Toast.LENGTH_SHORT).show();
        }

    }
}


版权声明
本文为[ZHAO_JH]所创,转载请带上原文链接,感谢