我如何能优化这个code包含一些重复的行? [英] How i can optimize this code that contain some repetitive line?

查看:128
本文介绍了我如何能优化这个code包含一些重复的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的Andr​​oid编程code

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 );

和它继续直到btn30
在蟒蛇我优化它由以下简单的code

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

在Java的编程我该怎么办呢?或者,我可以做到这一点?确实存在一个简单的code呢?

In java programing how i can do it?Or can I do it?do exist a easy code for it?

更新1

这样有两个办法做到这一点
code 1

SO there are two way to do it
Code 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)));
}

和另一种方法是GidView
坦克全部。

And other way is GidView
Tanks All.

推荐答案

您可以创建按钮的,并使用数组则getIdentifier 方法,它允许您通过它的名字来获得一个标识符。

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);
}

如果有人感兴趣了如何使用才达到相同的结果的Java只有

上述用途的解决方案安卓具体的方法(如 getResources 则getIdentifier ),不能在通常的使用​​的Java ,但我们可以用一个反射谱写方法就像一个则getIdentifier

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;
    }
}

然后:

final Button[] buttons = new Button[30];

for (int i = 0; i < buttons.length; i++) {
    buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}

这篇关于我如何能优化这个code包含一些重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆