如何在 Java 中为多个组合使用复选框开关?
How to use a switch on checkboxes for multiple combinations in Java?
这是我第一次 post 来这里,所以可能做得不太好...
我是一名软件开发专业的学生,现在我正在学习 Android 应用程序开发。
我被要求制作一个带有 4 个复选框的程序,所以当我选中其中任何一个时,该应用程序会显示特定图片。例如。
复选框 1:人。
复选框 2:汽车。
复选框 3:街道。
复选框 4:音乐。
如果我勾选了 1(Person) 和 2(Car),它应该在同一张图片中显示一个人和一辆汽车......我正在研究这个,我发现了这个 post。我认为这是制作这个程序的好方法,但我不知道如何让它正常工作。我试过这样做:
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>
但是当我 运行 应用程序时,它只显示默认图像(似乎只有 case 0b0000 有效?),即使我在 xml...我还尝试为每个复选框创建一个 onClick 事件,但似乎我没有以正确的方式使用模式变量。
如果我能得到帮助,我将不胜感激...我想我可以用 Ifs 做到这一点,但我个人对这种方式很感兴趣,我在 post 中看到了哈哈哈。
您需要将侦听器附加到您的复选框并将该模式检查移动到 onCheckedChanged()
方法中。这样,您的复选框可以通知 activity 并重新计算 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;
}
}
}
这是我第一次 post 来这里,所以可能做得不太好... 我是一名软件开发专业的学生,现在我正在学习 Android 应用程序开发。
我被要求制作一个带有 4 个复选框的程序,所以当我选中其中任何一个时,该应用程序会显示特定图片。例如。
复选框 1:人。
复选框 2:汽车。
复选框 3:街道。
复选框 4:音乐。
如果我勾选了 1(Person) 和 2(Car),它应该在同一张图片中显示一个人和一辆汽车......我正在研究这个,我发现了这个 post。我认为这是制作这个程序的好方法,但我不知道如何让它正常工作。我试过这样做:
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>
但是当我 运行 应用程序时,它只显示默认图像(似乎只有 case 0b0000 有效?),即使我在 xml...我还尝试为每个复选框创建一个 onClick 事件,但似乎我没有以正确的方式使用模式变量。
如果我能得到帮助,我将不胜感激...我想我可以用 Ifs 做到这一点,但我个人对这种方式很感兴趣,我在 post 中看到了哈哈哈。
您需要将侦听器附加到您的复选框并将该模式检查移动到 onCheckedChanged()
方法中。这样,您的复选框可以通知 activity 并重新计算 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;
}
}
}