CheckBox[] 与 onClickListener[]? [英] CheckBox[] with onClickListener[]?

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

问题描述

这个问题大多已关闭,我在此代码中遇到的唯一问题在此处

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

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?

此外,下面是我计划使用的 getter,我认为这部分可以正常工作:

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 extends 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);

然后使用传递给...的视图测试单击了哪个 CheckBox

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

onClick(View v)

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

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