将 CheckBox 与按钮和开关一起使用

Using CheckBox's with button and switch

我试图用所选 CheckBox 的颜色名称显示 toast,但是当我单击按钮时,没有任何反应,我尝试使用 LinearLayout 作为所有视图的父级来获取选择 Checkbox,首先我尝试在 main activity 中将其创建为 Linearlayout 但是当我选择一个复选框并单击按钮时应用程序停止,然后我将其更改为 View 并将其转换为 LinearLayout

这是我的代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = (LinearLayout)findViewById(R.id.view);

        Button btn = findViewById(R.id.button1);

        CheckBox chk1 = findViewById(R.id.chk1);
        CheckBox chk2 = findViewById(R.id.chk2);
        CheckBox chk3 = findViewById(R.id.chk3);

        btn.setOnClickListener(v -> {

            switch (view.getId()) {
                case R.id.chk1:
                    if (chk1.isChecked())
                        Toast.makeText(MainActivity.this, "Green", Toast.LENGTH_LONG).show();
                    break;

                case R.id.chk2:

                    if (chk2.isChecked())
                        Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();
                    break;

                case R.id.chk3:
                    if (chk3.isChecked())
                        Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();

                    break;
                default:
            }
        });

    }

布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:id="@+id/view"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_gravity="center"
        android:text="@string/app_name"
        android:textColor="@color/black"
        android:textAlignment="center"
        android:textSize="24sp"
        />

    <CheckBox
        android:id="@+id/chk1"
        android:text="Green"
        android:textSize="33sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </CheckBox>

    <CheckBox
        android:id="@+id/chk2"
        android:text="Orange"
        android:textSize="33sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </CheckBox>

    <CheckBox
        android:id="@+id/chk3"
        android:text="Blue"
        android:textSize="33sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </CheckBox>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_gravity="center"
        android:text="@string/button1"
        android:textAllCaps="false"
        android:textSize="24sp"
        >

    </Button>

</LinearLayout>
btn.setOnClickListener(v -> {
    switch (view.getId()) {
        case R.id.chk1:
            if (chk1.isChecked())
                Toast.makeText(MainActivity.this, "Green", Toast.LENGTH_LONG).show();
            break;
        case R.id.chk2:

            if (chk2.isChecked())
                Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();
            break;
        case R.id.chk3:
            if (chk3.isChecked())
                Toast.makeText(MainActivity.this, "Orange", Toast.LENGTH_LONG).show();
            break;
        default:
    }
});

根本原因

当用户单击 btn 按钮时,您检查 view 是否与 CheckBox 之一具有相同的 ID,但它始终为假,因为它们是不同的视图有不同的ID。这就是为什么您在屏幕上看不到任何吐司的原因。

解决方案

不用switch-case,只用if语句,像这样

btn.setOnClickListener(v -> {
    StringBuilder color = new StringBuilder();

    if (chk1.isChecked()) {
        color.append(chk1.getText().toString()).append(" ");
    }

    if (chk2.isChecked()) {
        color.append(chk2.getText().toString()).append(" ");
    }

    if (chk3.isChecked()) {
        color.append(chk3.getText().toString());
    }

    Toast.makeText(MainActivity.this, color.toString().trim(), Toast.LENGTH_LONG).show();
});

注意:如果您希望用户一次可以select一种颜色,请使用RadioButton代替CheckBox

更新:如果你真的想使用 switch-case 和 CheckBox,那么这里有一个解决方案。

public class MainActivity extends AppCompatActivity {

    private CheckBox latestCheckedCheckBox;
    private View.OnClickListener onCheckBoxClickedListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox checkBox = (CheckBox) v;
            if (checkBox.isChecked()) {
                latestCheckedCheckBox = checkBox;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view = (LinearLayout) findViewById(R.id.view);

        Button btn = findViewById(R.id.button1);

        CheckBox chk1 = findViewById(R.id.chk1);
        CheckBox chk2 = findViewById(R.id.chk2);
        CheckBox chk3 = findViewById(R.id.chk3);

        chk1.setOnClickListener(onCheckBoxClickedListener);
        chk2.setOnClickListener(onCheckBoxClickedListener);
        chk3.setOnClickListener(onCheckBoxClickedListener);

        btn.setOnClickListener(v -> {
            if (latestCheckedCheckBox == null) {
                return;
            }

            String color = "";
            switch (latestCheckedCheckBox.getId()) {
                case R.id.chk1:
                    if (chk1.isChecked()) {
                        color = chk1.getText().toString();
                        Toast.makeText(MainActivity.this, color, Toast.LENGTH_LONG).show();
                    }
                    break;
                case R.id.chk2:
                    if (chk2.isChecked()) {
                        color = chk2.getText().toString();
                        Toast.makeText(MainActivity.this, color, Toast.LENGTH_LONG).show();
                    }
                    break;
                case R.id.chk3:
                    if (chk3.isChecked()) {
                        color = chk3.getText().toString();
                        Toast.makeText(MainActivity.this, color, Toast.LENGTH_LONG).show();
                    }
                    break;
                default:
                    break;
            }
        });
    }
}