[英]How to use a switch on checkboxes for multiple combinations in Java?
This is my first post here so it may not be so well done... I'm a software development student and right now I'm learning Android Apps development.这是我在这里的第一篇文章,所以可能做得不太好......我是一名软件开发学生,现在我正在学习 Android 应用程序开发。
I was asked to make a program with 4 checkboxes so when I checked any of them, the app shows a certain picture.我被要求制作一个带有 4 个复选框的程序,所以当我选中其中任何一个时,应用程序会显示一张特定的图片。 For example.例如。
Checkbox 1: Person.复选框 1:人员。
Checkbox 2: Car.复选框 2:汽车。
Checkbox 3: Street.复选框 3:街道。
Checkbox 4: Music.复选框 4:音乐。
If I checked 1(Person) and 2(Car), it should show a person and a car in the same picture... I was researching about this and I found this post .如果我检查了 1(人)和 2(汽车),它应该在同一张照片中显示一个人和一辆车......我正在研究这个,我发现了这个帖子。 And I thought it was a good way to make this program, but I don't know how to make it work correctly.而且我认为这是制作这个程序的好方法,但我不知道如何让它正常工作。 I tried doing this:我试过这样做:
MainActivity.Java: 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: 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>
But when I run the application, it only shows the default image(seems like only case 0b0000 works?), even if I make a specific checkbox checked="true" in the xml... I also tried making an onClick event for each checkbox to have it, but it seems like I'm not using the pattern variable the right way.但是当我运行应用程序时,它只显示默认图像(似乎只有案例 0b0000 有效?),即使我在 xml 中设置了一个特定的复选框 check="true" ...我也尝试为每个复选框来拥有它,但似乎我没有以正确的方式使用模式变量。
I would be very thankful if I get help... I think I could do it with Ifs, but I'm personally interested in that way I read in the post hahaha.如果我能得到帮助,我会非常感激......我想我可以用 Ifs 来做,但我个人对我在帖子中读到的那种方式感兴趣哈哈哈。
You need to attach listener to your checkboxes and move that pattern checking inside of onCheckedChanged()
method.您需要将侦听器附加到您的复选框并将该模式检查移动到onCheckedChanged()
方法中。 In this way, your checkboxes can notify activity and recalculate value of pattern
:这样,您的复选框可以通知活动并重新计算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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.