2011-03-13 69 views
2

如果我知道变量的模式,如R.id.edit_x,其中x(1..N),如何获得对给定EditText的引用,如findViewByID(R.id.edit_1)。有没有像Dalvik中的“eval”函数?谢谢。Dalvik中的Eval函数

回答

0

hoha的答案是好的。你可以做的另一件事是创建一个查找表,将1..N映射到资源ID。 (大概您提前知道所有资源ID。)

+0

其实EditTexts创建程序所以没有ID是先前已知的! 。 – xain 2011-03-13 11:32:31

0

也许,您可以检查roboguice。它是一个android的ioc框架,它非常易于使用。我从样本复制一些代码从项目来展示如何使用它:

public class AstroboyMasterConsole extends RoboActivity { 

    @InjectView(R.id.self_destruct) Button selfDestructButton; 
    @InjectView(R.id.say_text)  EditText sayText; 
    @InjectView(R.id.brush_teeth) Button brushTeethButton; 
    @InjectView(tag="fightevil") Button fightEvilButton;  // we can also use tags if we want 

} 

那么你可以在你的代码,这些注入的变量