Java 哪个更有效
这是我为分配一组连续按钮而想到的。我喜欢这种风格,但是使用“按id查找”来执行单个按钮会更有效吗Java 哪个更有效,java,android,arrays,Java,Android,Arrays,这是我为分配一组连续按钮而想到的。我喜欢这种风格,但是使用“按id查找”来执行单个按钮会更有效吗 for(int i = 0; i < NanoConstants.NUMBER_OF_BUTTONS;i++){ //assign buttons Button addButton = (Button) findViewById(getResources().getIdentifier("project_button_" + String.valueOf(i), "id
for(int i = 0; i < NanoConstants.NUMBER_OF_BUTTONS;i++){ //assign buttons
Button addButton = (Button) findViewById(getResources().getIdentifier("project_button_" + String.valueOf(i), "id", getPackageName()));
addButton.setText(getResources().getStringArray(R.array.project_names)[i]);
addButton.setOnClickListener(this);
mProjectButtons.add(addButton);
}
for(int i=0;i
谢谢大家 由于此代码在应用程序启动时似乎只运行一次,因此我将避免尝试对其进行微优化。坚持使代码最具可读性的风格。由于此代码在应用程序启动时似乎只运行一次,因此我将避免尝试对其进行微优化。坚持让代码最具可读性的风格。不,不会<代码>for循环与展开的版本一样高效,可读性更好,并减少代码和复杂性。不,不会
for
循环与展开的版本一样高效,可读性更好,并减少代码和复杂性。您可以在组视图中添加按钮,并使用getChildAt循环其子对象
并检查视图是否为按钮,如下图所示:int childCnt = viewGroup.getChildCount();
for(int i=0;i<childCnt;i++){
View view = viewGroup.getChildAt(i);
if(view instanceof Button){
Button button = (Button)view;
addButton.setText(getResources().getStringArray(R.array.project_names)[i]);
addButton.setOnClickListener(this);
mProjectButtons.add(addButton);
}
}
int childCnt=viewGroup.getChildCount();
对于(inti=0;i,您可以在groupview中添加按钮,并使用getChildAt循环其child
并检查视图是否为按钮,如下图所示:
int childCnt = viewGroup.getChildCount();
for(int i=0;i<childCnt;i++){
View view = viewGroup.getChildAt(i);
if(view instanceof Button){
Button button = (Button)view;
addButton.setText(getResources().getStringArray(R.array.project_names)[i]);
addButton.setOnClickListener(this);
mProjectButtons.add(addButton);
}
}
int childCnt=viewGroup.getChildCount();
对于(int i=0;i