[英]How to use a switch on checkboxes for multiple combinations in 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 中設置了一個特定的復選框 check="true" ...我也嘗試為每個復選框來擁有它,但似乎我沒有以正確的方式使用模式變量。
如果我能得到幫助,我會非常感激......我想我可以用 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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.