如何在 Java 中为多个组合使用复选框开关?



这是我在这里的第一篇文章,所以可能做得不太好...... 我是一名软件开发专业的学生,现在我正在学习Android应用程序开发。

我被要求制作一个带有 4 个复选框的程序,因此当我选中其中任何一个时,应用程序会显示特定的图片。例如。

复选框 1:人员。

复选框 2:汽车。

复选框 3:街道。

复选框 4:音乐。

如果我选中 1(人(和 2(汽车(,它应该在同一张照片中显示一个人和一辆汽车......我正在研究这个,我找到了这篇文章。我认为这是制作这个程序的好方法,但我不知道如何让它正常工作。我尝试这样做:

MainActivity.Java:

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
CheckBox cb1, cb2, cb3, cb4;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = findViewById(R.id.persona);
cb2 = findViewById(R.id.car);
cb3 = findViewById(R.id.calle);
cb4 = findViewById(R.id.music);
img = findViewById(R.id.imagen);
int pattern = (cb1.isSelected() ? 0b0001 : 0)
| (cb2.isSelected() ? 0b0010 : 0)
| (cb3.isSelected() ? 0b0100 : 0)
| (cb4.isSelected() ? 0b1000 : 0);
switch (pattern) {
// No selection
case 0b0000:
img.setImageResource(R.drawable.def);
break;
//Person
case 0b0001:
img.setImageResource(R.drawable.wick);
break;
//Car
case 0b0010:
img.setImageResource(R.drawable.car);
break;
}
}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity"
android:id="@+id/rl">
<CheckBox
android:id="@+id/persona"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/persona"
android:layout_marginTop="25dp"
android:checked="false"
android:textSize="30sp"
/>
<CheckBox
android:id="@+id/car"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/car"
android:layout_below="@id/persona"
android:checked="false"
android:textSize="30sp"
/>
<CheckBox
android:id="@+id/calle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calle"
android:layout_below="@id/car"
android:checked="false"
android:textSize="30sp"
/>
<CheckBox
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/music"
android:layout_below="@id/calle"
android:checked="false"
android:textSize="30sp"
/>
<ImageView
android:id="@+id/imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/music"
android:layout_centerHorizontal="true"
android:contentDescription="@string/imagen" />
</RelativeLayout>

但是当我运行应用程序时,它只显示默认图像(似乎只有案例 0b0000 有效?(,即使我在 xml 中选中了特定的复选框="true"......我还尝试为每个复选框创建一个 onClick 事件来拥有它,但似乎我没有以正确的方式使用模式变量。

如果我得到帮助,我将不胜感激...我想我可以用 Ifs 做到这一点,但我个人对我在帖子中阅读的方式感兴趣哈哈哈。

您需要将侦听器附加到复选框,并将该模式检查移动到onCheckedChanged()方法内部。这样,您的复选框可以通知活动并重新计算pattern的值:

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox cb1, cb2, cb3, cb4;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 =  findViewById(R.id.persona);
cb2 = findViewById(R.id.car);
cb3 = findViewById(R.id.calle);
cb4 = findViewById(R.id.music);
img = findViewById(R.id.imagen);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
int pattern = (cb1.isChecked() ? 0b0001 : 0)
| (cb2.isChecked() ? 0b0010 : 0)
| (cb3.isChecked() ? 0b0100 : 0)
| (cb4.isChecked() ? 0b1000 : 0);
switch (pattern) {
// No selection
case 0b0000:
img.setImageResource(R.drawable.def);
break;
//Person
case 0b0001:
img.setImageResource(R.drawable.wick);
break;
//Car
case 0b0010:
img.setImageResource(R.drawable.car);
break;
}
}
}

最新更新