当前位置: 代码迷 >> Android >> Android中创办100按钮, 并且通过控件ID获得控件对象
  详细解决方案

Android中创办100按钮, 并且通过控件ID获得控件对象

热度:60   发布时间:2016-05-01 13:01:02.0
Android中创建100按钮, 并且通过控件ID获得控件对象
我想在一个Activity中创建100个按钮,除了在layout的xml文件中手动写100个按钮的代码外, 还有没有的别的更高效的方法?

如果我在java文件中通过findViewById()方法来获得这100按钮, 除了说 100个findViewById()之外, 还有没有别的更高效的方法?

------解决方案--------------------
定义一个按钮,用for语句获得,并存入数组中
------解决方案--------------------
还是用软件生成吧
------解决方案--------------------
Java code
先找到你的main.xml根节点LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);for(int i=0;i<100;i++){Button button = new Button(this);                            button.setText(String.ValueOf(i));                            button.setTextSize(18);button.setWidth(20)button.setHeight(20);button.setId(i);linearLayout.addView(button);}//查找for(int i=0;i<100;i++){ArrayList list = new ArrayList<Button>();Button btn = (Button)linearLayout.findViewById(i);list.add(btn);}
------解决方案--------------------
楼上忌讳for循环里面new那么多对象
------解决方案--------------------
探讨

楼上忌讳for循环里面new那么多对象

------解决方案--------------------
不new一百个对象怎么得到一百个button啊,求教
  相关解决方案