带有 onClickListener[] 的 CheckBox[]? [英] CheckBox[] with onClickListener[]?

查看:23
本文介绍了带有 onClickListener[] 的 CheckBox[]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题基本上已经结束,我在这段代码中遇到的唯一问题是这里

对于我的应用程序的一部分,我有一页以复选框表示的项目,每个项目都有一个关联的布尔值,最终被收集并存储为字符串,如下所示:

For part of my app, I have a page of items that are represented as checkboxes, each with a associated boolean that eventually get collected and stored as a string as follows:

final CheckBox gas_oil = (CheckBox) findViewById(R.id.gas_oil);
gas_oil.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
    {
    if (gas_oil.isChecked())
        {
        impacts.append(getString(R.string.gas_oil) + " | ");
        anythingchecked = true;
        }
    }
});

这是非常乏味的,而且似乎不是一种非常有效的方法,因为我有 9 或 10 项用户可以检查或不检查的项目.此方法还意味着,如果他们单击并取消单击某些内容,则该项目仍在 StringBuilder 影响 中,并且如果他们再次单击它,则它在那里两次.

this is extremely tedious and did not seem to be a very efficent way to do this since i have 9 or 10 items that users can check or not. also this method means that if they click and unclick something, that item is still in the StringBuilder impacts and that if they click it again then it is in there twice.

我的解决方案是将所有内容都放在数组中:

My solution was to have everything in arrays:

  String[] impactsn = getResources().getStringArray(R.array.impacts);
  final boolean[] impactsb = new boolean[impactsn.length];
  final CheckBox[] impactsc = new CheckBox[impactsn.length];
  View[] impactsv = new View[]{findViewById(R.id.gas_oil),findViewById(R.id.ghost_fishing),findViewById(R.id.marsh_damage),findViewById(R.id.nav_haz),findViewById(R.id.shell_damage),findViewById(R.id.waste_pollution),findViewById(R.id.wild_entang),findViewById(R.id.other)};

  for (int i = 0; i < impactsn.length; i++)
    {
    impactsc[i] = (CheckBox) impactsv[i];
    impactsc[i].setOnClickListener(new OnClickListener()
            {   
            @Override
            public void onClick(View v)
                {
                if (impactsc[i].isChecked())
                    impactsb[i] = true;
                else
                    impactsb[i] = false;
                }
            });
    }

不幸的是,这样做会导致问题(据我了解)OnClickListener 中的内容必须是最终的.由于编写的代码,i 永远不会是最终的,所以我有点停滞不前.

unfortunately doing this causes the problem that (as far as i understand it) things within an OnClickListener have to be final. With the code as written, i can never be final, so I'm sort of at a standstill.

应该/我也可以有一个 OnClickListeners 数组吗?我应该调用我这里的代码之外的方法吗?

Should/can I have an array of OnClickListeners as well? Should I be calling to a method outside of the code I have here?

另外,下面是我计划使用的吸气剂,我认为这部分可以正常工作:

Also, below is the getter i was planning on using, I think that part will work just fine:

String getImpacts ()
    {
            String[] impactsn = getResources().getStringArray(R.array.impacts);
    StringBuilder impactss = new StringBuilder();
    for (int i = 0; i < impactsn.length; i ++)
        {
        if (impactsb[i])
                    impactss.append(impactsn[i] + " | ");
        }
    return String.valueOf(impactss);
    }

这是我现在运行的代码版本:

包 com.citsci.mardeb;

package com.citsci.mardeb;

导入android.app.Activity;导入android.content.res.Resources;导入android.os.Bundle;导入android.view.View;导入 android.widget.CheckBox;导入android.widget.EditText;

import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText;

公共类 Impacts 扩展 Activity 实现 View.OnClickListener{整数长度 = 7;boolean[] Impactb = new boolean[] {false, false, false, false, false, false, false, false};编辑文本视图;

public class Impacts extends Activity implements View.OnClickListener { int length = 7; boolean[] impactsb = new boolean[] {false, false, false, false, false, false, false, false}; EditText view;

public void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.impacts);

//for (int i = 0; i

// for (int i = 0; i < length; i++) // impactsb[i] = false;

  View[] impactsv = new View[]
     {
    findViewById(R.id.gas_oil),
        findViewById(R.id.ghost_fishing),
        findViewById(R.id.marsh_damage),
        findViewById(R.id.nav_haz),
        findViewById(R.id.shell_damage),
        findViewById(R.id.waste_pollution),
        findViewById(R.id.wild_entang),
        findViewById(R.id.other)
        };

  CheckBox[] impactsc = new CheckBox[length];

  for (int i = 0; i < length; i++)
     {
     impactsc[i] = (CheckBox) impactsv[i];
     impactsc[i].setOnClickListener(this);
     }

}// end of onCreate

@Override
public void onClick(View v)
    {
  switch (v.getId()) {
     case (R.id.gas_oil):
        impactsb[0] =! impactsb[0];
        break;
     case (R.id.ghost_fishing):
        impactsb[1] =! impactsb[1];
        break;
     case (R.id.marsh_damage):
        impactsb[2] =! impactsb[2];
        break;
     case (R.id.nav_haz):
        impactsb[3] =! impactsb[3];
        break;
     case (R.id.shell_damage):
        impactsb[4] =! impactsb[4];
        break;
     case (R.id.waste_pollution):
        impactsb[5] =! impactsb[5];
        break;
     case (R.id.wild_entang):
        impactsb[6] =! impactsb[6];
        break;
     case (R.id.other):
        impactsb[7] =! impactsb[7];
        }
    }

String getImpacts ()
    {
    String[] impactsn = new String[length];
    Resources myResources = getResources();
    impactsn = myResources.getStringArray(R.array.impacts);
  StringBuilder impactss = new StringBuilder();
    for (int i = 0; i < length; i ++)
        {
        if (impactsb[i])
            impactss.append(impactsn[i] + " | ");
        }
    if (String.valueOf(impactss) != "")
        impactss.insert(0, "Impacts: ");
    return String.valueOf(impactss);
    }
}// end of Impacts.class

推荐答案

在这种情况下不要使用匿名(内联)侦听器.而是让您的 Activity 实现侦听器...

Don't use anonymous (inline) listeners in this case. Instead have your Activity implement the listener...

public class MyActivity extends Activity
    implements View.OnClickListener {

    ...

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case impactsv[0].getId:
                impactsb[0] = !impactsb[0];
                ...
                break;

            // Add other cases here

        }

    }
}

那么设置监听器所需要做的就是……

Then all you need to do to set the listener is...

impactsc[i].setOnClickListener(this);

然后使用传递给的 View 来测试点击了哪个 CheckBox...

Then test for which CheckBox has been clicked by using the View which is passed to...

onClick(查看 v)

这篇关于带有 onClickListener[] 的 CheckBox[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆