当前位置:编程学习 > wap >>

怎样定义一个Button控件数组?

我需要用到很多Button控件,所以想定义一个数组。
先声明了
Button []button;

再取得Button对象时就报错,是不是要先new一个对象?
 
button[0]=(Button)findViewById(R.id.btton01);
 button[1]=(Button)findViewById(R.id.button01_1);
 button[2]=(Button)findViewById(R.id.button01_2);
 button[3]=(Button)findViewById(R.id.button01_3);
 button[4]=(Button)findViewById(R.id.button01_4);
--------------------编程问答-------------------- findViewById 找的是Layout已经有的,如果没有,肯定找不出来啊 --------------------编程问答-------------------- 先new一个button[0]=new Button() --------------------编程问答--------------------
引用 2 楼 anbam 的回复:
先new一个button[0]=new Button()


是需要先new一个button对象。 --------------------编程问答-------------------- 如果layout里面你写了button的话可以findviewbyid
没写的话,就必须new一个button了 --------------------编程问答-------------------- 还是一个一个按钮定义比较保险,去一些android技术论坛看看,应该可以找到很多资料,www.connectionwe.com安卓航班网 --------------------编程问答-------------------- 肯定得先给button数组new一个对象,并且设置一个大小,然后再通过button[index]进行赋值 --------------------编程问答-------------------- 弄个ArrayList<Button> mlist;不就行了? --------------------编程问答--------------------

private ArrayList<Button> mlist = new List<Button>();
mlist.add((Button)findViewById(R.id.btton01));
mlist.add((Button)findViewById(R.id.button01_1));
mlist.add((Button)findViewById(R.id.button01_2));
mlist.add((Button)findViewById(R.id.button01_3));
mlist.add((Button)findViewById(R.id.button01_4));
--------------------编程问答-------------------- Button []button;

都没分配空间你怎么给它赋值?

Button[] button = new Button[4]
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,