简体   繁体   English

ANDROID处理大量带有数组的按钮吗?

[英]ANDROID Handle large amount of buttons with array?

Hey I am making an android app that will have ~256 buttons. 嘿,我正在制作一个具有〜256个按钮的android应用。 Because I dont want to write the very same code for everyone of these I thought it might be possible to realize an easier solution via arrays. 因为我不想为每个人都编写完全相同的代码,所以我认为通过数组实现更简单的解决方案是可能的。 My approach in the onCreate to set the listeners was: 我在onCreate中设置侦听器的方法是:

1    for (int i=1; i<32; i++)
2               {
3                   button[i] = (Button)findViewById(R.id.button[i]);
4                   button[i].setOnTouchListener(this);
5               }

I set the Button[] like that: Button[] button=new Button [64]; 我这样设置Button []: Button[] button=new Button [64];

Now, eclipse tells me in line 3 "button cannot be resolved or is not a field" and it just underlines the word "button", so I think it ignores/just does not recognize the [i] (array)-stuff. 现在,eclipse在第3行告诉我“按钮无法解析或不是字段”,它只是在单词“ button”下划线,因此我认为它忽略/只是不识别[i](数组)填充。

The rest of my code seems to get on with that perfectly because it gets recognized as an object (correct me if I said that wrong) but the findViewById() doesn't get on with it .. 我的其余代码似乎可以完美地进行下去,因为它被识别为对象(如果我说错了,请纠正我),但是findViewById()不能继续进行下去。

Thanks for the replies, Alex 感谢您的回复,Alex

You can't do what you proposed in your solution. 您无法完成您在解决方案中提出的建议。 A better way to go about it is to add the buttons dynamically in code. 更好的方法是在代码中动态添加按钮。 For instance, 例如,

View parentView = (LinearLayout) findViewById(R.id.parentView);
// declare button array above
for (int i=1; i<32; i++)
{
    Button btn = new Button(context);
    // EDIT: adding a background resource
    btn.setBackgroundResource(R.layout.button_layout);
    btn.setText("This is my text");
    btn.setOnTouchListener(this);
    button[i] = btn;
}

User "Horschtele" answered it in a perfect way but he deleted his answer on his own (don't know why). 用户“ Horschtele”以完美的方式回答了问题,但他自己删除了答案(不知道为什么)。

Horschtele, if you read that, I just want to say that this solution is just perfect! Horschtele,如果您读过那本书,我只是想说这个解决方案是完美的!

I have to (or at least I think I have to) do this for every tableRow but this saves me an infinite amount of time. 我必须(或至少我认为必须)对每个tableRow进行此操作,但这可以为我节省大量时间。 Thanks again Horschtele (are you german? :)) 再次感谢Horschtele(您是德国人吗?

My modified version of Horschtele's answer if you already have your buttons in a table: 如果您已经在表格中放置了按钮,那么我对Horschtele答案的修改版本:

ViewGroup container = (ViewGroup) findViewById(R.id.tableRow1);

            for(int i=0; i<container.getChildCount();i++){
                System.out.println(container.getChildCount());
            Button button = (Button)container.getChildAt(i);
            button.setOnTouchListener(this);
            }

(don't wonder about the println, you can easily check if the system correctly recognizes the container you are refering to). (不要怀疑println,您可以轻松地检查系统是否正确识别了您所引用的容器)。

If you did it my way with an array of Button then this is the way to go: 如果您使用Button数组来做到这一点,那么方法就是这样:

button[i] = (Button)container.getChildAt(i);
button[i].setOnTouchListener(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM