I have below code in android programming
Button btn1 = ( Button ) findViewById( R.id.btn1 );
Button btn2 = ( Button ) findViewById( R.id.btn2 );
Button btn3 = ( Button ) findViewById( R.id.btn3 );
Button btn4 = ( Button ) findViewById( R.id.btn4 );
Button btn5 = ( Button ) findViewById( R.id.btn5 );
Button btn6 = ( Button ) findViewById( R.id.btn6 );
Button btn7 = ( Button ) findViewById( R.id.btn7 );
Button btn8 = ( Button ) findViewById( R.id.btn8 );
Button btn9 = ( Button ) findViewById( R.id.btn9 );
And it continue untill btn30
In python I optimize it by below easy code
#is a python syntax (for_statement)
#python work by tab
for i in range(1,31):
#in python not need to declare temp
temp="""Button btn"""+str(i)+"""=(Button)findViewById(R.id.btn"""+str(i)+""")"""
exec(temp)#a default function in python
In java programing how i can do it?Or can I do it?do exist a easy code for it?
UPDATE 1
SO there are two way to do itCode 1
:
final int number = 30;
final Button[] buttons = new Button[number];
final Resources resources = getResources();
for (int i = 0; i < number; i++) {
final String name = "btn" + (i + 1);
final int id = resources.getIdentifier(name, "id", getPackageName());
buttons[i] = (Button) findViewById(id);
}
Code 2
:
public static int getIdByName(final String name) {
try {
final Field field = R.id.class.getDeclaredField(name);
field.setAccessible(true);
return field.getInt(null);
} catch (Exception ignore) {
return -1;
}
}
final Button[] buttons = new Button[30];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
And other way is GidView
You can create an array of Button
's and use getIdentifier
method that allows you to get an identifier by its name.
final int number = 30;
final Button[] buttons = new Button[number];
final Resources resources = getResources();
for (int i = 0; i < number; i++) {
final String name = "btn" + (i + 1);
final int id = resources.getIdentifier(name, "id", getPackageName());
buttons[i] = (Button) findViewById(id);
}
In case someone is interested how to achive the same result using Java only
The solution above uses Android
specific methods (such as getResources
, getIdentifier
) and can not be used in usual Java
, but we can use a reflection
and write a method that works like a getIdentifier
:
public static int getIdByName(final String name) {
try {
final Field field = R.id.class.getDeclaredField(name);
field.setAccessible(true);
return field.getInt(null);
} catch (Exception ignore) {
return -1;
}
}
And then:
final Button[] buttons = new Button[30];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
Instead of optimizing this kind of code you should rethink your layout. If you have 30 buttons on the screen, a ListView
is probably the better solution. You can access the items via index and handle onClick
events just like with the buttons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With