我在Android Eclipse上有这个代码,我需要缩短代码以减小文件大小我认为可以使用for方法缩短代码,但我是业余爱好者:
Button P1 = (Button) findViewById(R.id.Part1);
Button P2 = (Button) findViewById(R.id.Part2);
Button P3 = (Button) findViewById(R.id.Part3);
Button P4 = (Button) findViewById(R.id.Part4);
Button P5 = (Button) findViewById(R.id.Part5);
Button P6 = (Button) findViewById(R.id.Part6);
Button P7 = (Button) findViewById(R.id.Part7);
Button P8 = (Button) findViewById(R.id.Part8);
Button P9 = (Button) findViewById(R.id.Part9);
Button P10 = (Button) findViewById(R.id.Part10);
Button P11 = (Button) findViewById(R.id.Part11);
Button P12 = (Button) findViewById(R.id.Part12);
Button P13 = (Button) findViewById(R.id.Part13);
Button P14 = (Button) findViewById(R.id.Part14);
Button P15 = (Button) findViewById(R.id.Part15);
Button P16 = (Button) findViewById(R.id.Part16);
Button P17 = (Button) findViewById(R.id.Part17);
Button P18 = (Button) findViewById(R.id.Part18);
Button P19 = (Button) findViewById(R.id.Part19);
Button P20 = (Button) findViewById(R.id.Part20);
Button P21 = (Button) findViewById(R.id.Part21);
Button P22 = (Button) findViewById(R.id.Part22);
Button P23 = (Button) findViewById(R.id.Part23);
Button P24 = (Button) findViewById(R.id.Part24);
Button P25 = (Button) findViewById(R.id.Part25);
Button P26 = (Button) findViewById(R.id.Part26);
Button P27 = (Button) findViewById(R.id.Part27);
Button P28 = (Button) findViewById(R.id.Part28);
Button P29 = (Button) findViewById(R.id.Part29);
Button P30 = (Button) findViewById(R.id.Part30);
Button P31 = (Button) findViewById(R.id.Part31);
Button P32 = (Button) findViewById(R.id.Part32);
Button P33 = (Button) findViewById(R.id.Part33);
Button P34 = (Button) findViewById(R.id.Part34);
Button P35 = (Button) findViewById(R.id.Part35);
Button P36 = (Button) findViewById(R.id.Part36);
Button P37 = (Button) findViewById(R.id.Part37);
Button P38 = (Button) findViewById(R.id.Part38);
Button P39 = (Button) findViewById(R.id.Part39);
Button P40 = (Button) findViewById(R.id.Part40);
Button P41 = (Button) findViewById(R.id.Part41);
Button P42 = (Button) findViewById(R.id.Part42);
Button P43 = (Button) findViewById(R.id.Part43);
Button P44 = (Button) findViewById(R.id.Part44);
Button P45 = (Button) findViewById(R.id.Part45);
Button P46 = (Button) findViewById(R.id.Part46);
Button P47 = (Button) findViewById(R.id.Part47);
Button P48 = (Button) findViewById(R.id.Part48);
Button P49 = (Button) findViewById(R.id.Part49);
Button P50 = (Button) findViewById(R.id.Part50);
Button P51 = (Button) findViewById(R.id.Part51);
Button P52 = (Button) findViewById(R.id.Part52);
Button P53 = (Button) findViewById(R.id.Part53);
Button P54 = (Button) findViewById(R.id.Part54);
Button P55 = (Button) findViewById(R.id.Part55);
Button P56 = (Button) findViewById(R.id.Part56);
Button P57 = (Button) findViewById(R.id.Part57);
Button P58 = (Button) findViewById(R.id.Part58);
Button P59 = (Button) findViewById(R.id.Part59);
Button P60 = (Button) findViewById(R.id.Part60);
Button P61 = (Button) findViewById(R.id.Part61);
Button P62 = (Button) findViewById(R.id.Part62);
Button P63 = (Button) findViewById(R.id.Part63);
Button P64 = (Button) findViewById(R.id.Part64);
Button P65 = (Button) findViewById(R.id.Part65);
Button P66 = (Button) findViewById(R.id.Part66);
Button P67 = (Button) findViewById(R.id.Part67);
Button P68 = (Button) findViewById(R.id.Part68);
Button P69 = (Button) findViewById(R.id.Part69);
Button P70 = (Button) findViewById(R.id.Part70);
Button P71 = (Button) findViewById(R.id.Part71);
Button P72 = (Button) findViewById(R.id.Part72);
Button P73 = (Button) findViewById(R.id.Part73);
Button P74 = (Button) findViewById(R.id.Part74);
Button P75 = (Button) findViewById(R.id.Part75);
Button P76 = (Button) findViewById(R.id.Part76);
Button P77 = (Button) findViewById(R.id.Part77);
Button P78 = (Button) findViewById(R.id.Part78);
Button P79 = (Button) findViewById(R.id.Part79);
Button P80 = (Button) findViewById(R.id.Part80);
Button P81 = (Button) findViewById(R.id.Part81);
Button P82 = (Button) findViewById(R.id.Part82);
Button P83 = (Button) findViewById(R.id.Part83);
Button P84 = (Button) findViewById(R.id.Part84);
Button P85 = (Button) findViewById(R.id.Part85);
Button P86 = (Button) findViewById(R.id.Part86);
Button P87 = (Button) findViewById(R.id.Part87);
Button P88 = (Button) findViewById(R.id.Part88);
Button P89 = (Button) findViewById(R.id.Part89);
非常感谢,我需要一个代码来缩短代码。
答案 0 :(得分:1)
如果父布局仅包含您可以执行的按钮视图
for(int i = 0; i < layout.getChildCount(); i++) {
Button button = (Button)layout.getChildAt(i);
}
答案 1 :(得分:0)
如果所有按钮共享通用类型的功能和UI,则应使用RecyclerView
或某些ListView
类型与适当的适配器。适配器适用于此类任务。请上传您应用的ScreenShots,以便更好地了解问题。但有一点可以肯定,找到90个按钮ID不是一个好方法。
答案 2 :(得分:0)
您应该考虑使用ListView或RecyclerView进行列表布局。
或者让我们使用数据绑定。使用数据绑定,您可以避免使用findById
方法。
https://developer.android.com/topic/libraries/data-binding/index.html
在build.gradle中:
android {
....
dataBinding {
enabled = true
}
}
在您的布局文件中,使用以下标记:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
... your original layout tags ...
</layout>
在java程序中,像这样使用DataBindingUtil:
MainActivityBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
//you can access buttons like this:
binding.Part1.setOnClickListener(...);
}