【问题标题】:Android Java Multiple ImageButton array OnClick and toggle background colourAndroid Java 多 ImageButton 数组 OnClick 和切换背景颜色
【发布时间】:2021-02-12 04:33:12
【问题描述】:

我已经尝试了一些不同的解决方案来解决这个问题,但仍然没有找到合适的智能解决方案。在活动中将有多达 20 个 ImageButton,当用户按下按钮时,背景会改变颜色,这一切都非常简单。但是做一些很简单的事情似乎需要很多代码。

对于其他响应,您似乎可以从 ImageButton XML 中获得一个通用的 OnClick,但仍然需要检查每个 ImageButton 的大量 switch 语句。我尝试了一组 OnClickListners(),但无法确定按下了哪个图像按钮。

如果唯一的方法是很长的“if”语句或“switch” case,那么只需复制并粘贴代码,但只是想知道是否有更简单、更智能的方法来管理这些多个 ImageButtons

    ElementButton0 = (ImageButton) findViewById(R.id.elementButton0);
    ElementButton1 = (ImageButton) findViewById(R.id.elementButton1);
    ElementButton2 = (ImageButton) findViewById(R.id.elementButton2);

    ElementButton0.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            if (ElementButton0.isPressed() == true) {
                if (elementbuttonstate0 == true)
                {
                    ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                    elementbuttonstate0 = false;
                } else {
                    ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
                    elementbuttonstate0 = true;
                }
            }

        }
    });

    ElementButton1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            if (ElementButton1.isPressed() == true) {
                if (elementbuttonstate1 == true)
                {
                    ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                    elementbuttonstate1 = false;
                } else {
                    ElementButton1.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
                    elementbuttonstate1 = true;
                }
            }

        }
    });
    ElementButton2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            if (ElementButton2.isPressed() == true) {
                if (elementbuttonstate2 == true)
                {
                    ElementButton2.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                    elementbuttonstate2 = false;
                } else {
                    ElementButton2.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
                    elementbuttonstate2 = true;
                }
            }

        }
    });

【问题讨论】:

    标签: javascript android


    【解决方案1】:

    我假设您在 xml 中创建了 20 个 ElementButton。我们可以摆脱它。 我们可以使用RecyclerView 实现来实现这一点。

    1. 使用RecyclerView 扩充20 个项目。
    2. 处理单击回收站查看项目。这将为您提供用户点击的position
    3. 使用您使用过的isPressed() 逻辑切换该点击元素的背景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 2019-03-10
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多