简体   繁体   中英

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.

I was asked to make a program with 4 checkboxes so when I checked any of them, the app shows a certain picture. For example.

Checkbox 1: Person.

Checkbox 2: Car.

Checkbox 3: Street.

Checkbox 4: Music.

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 . 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:

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>

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.

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.

You need to attach listener to your checkboxes and move that pattern checking inside of onCheckedChanged() method. In this way, your checkboxes can notify activity and recalculate value of 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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM