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

米萨尔·科尔特斯(MisaelCortés)

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

我被要求制作一个带有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>

但是,当我运行该应用程序时,即使我在xml中创建了一个特定的复选框checked =“ true”,它也仅显示默认图像(似乎仅适用于大小写0b0000的情况?)。复选框具有它,但似乎我没有以正确的方式使用模式变量。

如果能得到帮助,我将非常感激……我想我可以通过Ifs做到这一点,但是我个人对我在哈哈哈文章中阅读的方式感兴趣。

纳蒂格·巴巴耶夫(Natig Babayev)

您需要将侦听器附加到您的复选框,并将该模式​​检查移到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;
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在reactjs中使用antd多个复选框?

如何在反应中使用多个复选框并收集选中的复选框

如何在PHP中使用foreach循环在多个复选框中设置所选属性?

如何在CodeIgniter中使用复选框输入多个值?

如何在SQL查询中使用多个HTML复选框中的值?

如何在 python 中使用 Selenium 包单击多个复选框和下拉列表?

Javascript - 如何在 Vue.js 中使用多个复选框进行过滤?

如何在UPDATE查询php中使用多个复选框

如何在React钩子中使用CheckAll复选框创建复选框?

如何在Materialize表中使用复选框

如何在PHP中使用html复选框插入INTO

如何在复选框栏杆中使用枚举

如何在React中使用复选框形式?

如何在复选框中使用 css

如何在Python中使用Selenium选中复选框

如何在Android中使用单选复选框

如何在表格php中使用复选框

如何在我的 WordPress 网站中使用复选框?

如何在 jetpack compose 中使用复选框控件?

如何在Swing Java的复选框中使用全选选项?

如何在Java Swing中使用复选框列出列表?

如何在Android中使用api将多个选定的复选框值传递给服务器?

单击复选框后,如何在Vue.js中使用v-on传递复选框ID?

如何在css中使用复选标记作为复选框

在PHP中使用多个复选框进行分页

在React Native中使用多个复选框处理状态

mysqli select中使用的多个表单复选框结果

如何在aspxgridview中使复选框可编辑?

如何在离子框架中使ios的复选框方形?