使用带有按钮和开关的复选框

35g0bw71  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(380)

我试图用选中复选框的颜色名称来显示toast,但是当我单击按钮时,什么也没有发生,我试图使用linearlayout作为所有视图的父视图来获取选中复选框的id,首先,我尝试在主活动中将其创建为linearlayout,但当我选中一个复选框并单击按钮时,应用程序停止,然后我将其更改为视图并将其强制转换为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>
hrirmatl

hrirmatl1#

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的不同视图。这就是为什么在屏幕上看不到任何toast。
解决方案
不需要使用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();
});

注:如果您想让用户一次选择一种颜色,请使用 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;
            }
        });
    }
}

相关问题