复选框[]与onClickListener []?复选框、onClickListener

2023-09-04 12:45:56 作者:﹄輩囝守護祢乀

编辑:这个问题主要是封闭的,唯一的问题,我有这个code在这里

有关我的应用程序的一部分,我有重新psented为复选框$ P $项目的页面,每个与最终会收集并作为一个字符串存储在一个相关的布尔如下:

 最后复选框gas_oil =(复选框)findViewById(R.id.gas_oil);
gas_oil.setOnClickListener(新OnClickListener()
{
公共无效的onClick(视图v)
    {
    如果(gas_oil.isChecked())
        {
        impacts.append(的getString(R.string.gas_oil)+|);
        anythingchecked = TRUE;
        }
    }
});
 

这是非常繁琐的,并且没有似乎是一个非常efficent办法做到这一点,因为我有9个或10个项目,用户可以检查或不。这种方法也意味着,如果他们点击和unclick的东西,该项目尚处于 StringBuilder的影响键,如果他们再次单击它,然后它在那里的两倍。

我的解决办法是让所有的数组:

 的String [] impactsn = getResources()getStringArray(R.array.impacts)。
  最终布尔[] impactsb =新的布尔[impactsn.length]
  最后复选框[] impactsc =新的复选框[impactsn.length]
  查看[] impactsv =新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;我++)
    {
    impactsc [I] =(复选框)impactsv [I]
    impactsc [I] .setOnClickListener(新OnClickListener()
            {
            @覆盖
            公共无效的onClick(视图v)
                {
                如果(impactsc [I] .isChecked())
                    impactsb [我] =真;
                其他
                    impactsb [我] = FALSE;
                }
            });
    }
 
安卓里面,public void button onClick View view 中的参数有什么作用

不幸的是这样做会导致问题(据我所知)的东西在 OnClickListener 必须是最后一个。有了书面的code,永远是最终的,所以我排序处于停滞状态。

如果/我可以有 OnClickListeners 以及数组?我应该打电话给方法的code之外,我这里?

另外,下面是吸气我是计划使用,我认为这部分工作得很好:

 字符串getImpacts()
    {
            。的String [] impactsn = getResources()getStringArray(R.array.impacts);
    StringBuilder的impactss =新的StringBuilder();
    的for(int i = 0; I< impactsn.length;我++)
        {
        如果(impactsb [I])
                    impactss.append(impactsn [我] +|);
        }
    返回将String.valueOf(impactss);
    }
 

编辑:这是code IM与现在正在运行的版本:

包com.citsci.mardeb;

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

公共类的影响扩展了活动实施View.OnClickListener {     INT长度= 7;     布尔[] impactsb =新的布尔[] {假的,假的,假的,假的,假的,假的,假的,假};     的EditText视图;

 公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.impacts);
 

//的for(int i = 0; I<长度;我++) // impactsb [我] = FALSE;

 查看[] impactsv =新的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)
        };

  复选框[] impactsc =新的复选框[长度];

  的for(int i = 0; I<长度;我++)
     {
     impactsc [I] =(复选框)impactsv [I]
     impactsc [I] .setOnClickListener(本);
     }

} //的onCreate结束

@覆盖
公共无效的onClick(视图v)
    {
  开关(v.getId()){
     案例(R.id.gas_oil):
        impactsb [0] =! impactsb [0];
        打破;
     案例(R.id.ghost_fishing):
        impactsb [1] =! impactsb [1];
        打破;
     案例(R.id.marsh_damage):
        impactsb [2] =! impactsb [2];
        打破;
     案例(R.id.nav_haz):
        impactsb [3] =! impactsb [3];
        打破;
     案例(R.id.shell_damage):
        impactsb [4] =! impactsb [4];
        打破;
     案例(R.id.waste_pollution):
        impactsb [5] =! impactsb [5];
        打破;
     案例(R.id.wild_entang):
        impactsb [6] =! impactsb [6];
        打破;
     案例(R.id.other):
        impactsb [7] =! impactsb [7];
        }
    }

字符串getImpacts()
    {
    的String [] impactsn =新的String [长度];
    资源为MyResources = getResources();
    impactsn = myResources.getStringArray(R.array.impacts);
  StringBuilder的impactss =新的StringBuilder();
    的for(int i = 0; I<长度;我++)
        {
        如果(impactsb [I])
            impactss.append(impactsn [我] +|);
        }
    如果(将String.valueOf(impactss)!=)
        impactss.insert(0,影响:);
    返回将String.valueOf(impactss);
    }
} // Impacts.class结束
 

解决方案

不要在这种情况下,使用匿名(内联)的听众。相反,有你的活动实施监听器...

 公共类MyActivity扩展活动
    实现View.OnClickListener {

    ...

    @覆盖
    公共无效的onClick(视图v){

        开关(v.getId()){
            案impactsv [0] .getId:
                impactsb [0] = impactsb [0]!;
                ...
                打破;

            //添加其他情况下在这里

        }

    }
}
 

然后,所有你需要做的设置听者...

  impactsc [I] .setOnClickListener(本);
 

然后测试该复选框被点击使用由通过观...

的onClick(视图v)

EDIT: this question is mostly closed and the only problems i have with this code are discussed here

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

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

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.

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

EDIT: this is the version of code im running with now:

package com.citsci.mardeb;

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;

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

解决方案

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

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

onClick(View v)