我知道,Java没有一个pre处理器,所以有些东西是或多或少是不可能在Java中。
难道真有否的方式来填补这些阵列在一个循环动态的名字呢? 我想有这样的:
的for(int i = 0;我小于5;我++){
earnTvs [I] =(TextView中)findViewById(R.id.INSERT_GREAT_TRICK_HERE(赚+ I +电视));
}
而不是
earnTvs [0] =(TextView中)findViewById(R.id.earn1Tv);
earnTvs [1] =(TextView的)findViewById(R.id.earn2Tv);
earnTvs [2] =(TextView的)findViewById(R.id.earn3Tv);
earnTvs [3] =(TextView的)findViewById(R.id.earn4Tv);
earnTvs [4] =(TextView的)findViewById(R.id.earn5Tv);
timeTvs [0] =(TextView的)findViewById(R.id.time1Tv);
...
ownTvs [0] =(TextView的)findViewById(R.id.own1Tv);
...
costTvs [0] =(TextView的)findViewById(R.id.build1Tv);
...
buyBtns [0] =(的ImageButton)findViewById(R.id.buy1Bt);
...
progressBars [0] =(进度)findViewById(R.id.prog1pB);
...
buildBtns [0] =(按钮)findViewById(R.id.build1Bt);
...
或者有什么淫招,可使用?
解决方案我会做这样的:
最终诠释[] earnTvsId =新INT [] {R.id.earn1Tv,R.id.earn2Tv,R.id.earn3Tv,R.id.earn4Tv ...} ;
的for(int i = 0; I< earnTvsId.length ++我){
earnTvs [I] =(TextView中)findViewById(earnTvsId [I]);
}
如果你想使用则getIdentifier()
方法:
的for(int i = 0; I< NUMBER_OF_TEXTVIEWS ++我){
最终诠释渣油= getResources()则getIdentifier(赚+ I +电视,ID,getPackageName())。
earnTvs [I] =(TextView中)findViewById(渣油);
}
I know, that Java doesn't have a pre-processor, so some stuff is more or less impossible in java.
Is there really NO way to fill those arrays with dynamic names in a loop? I'd like to have something like:
for(int i=0;i<5;i++){
earnTvs[i]=(TextView) findViewById(R.id.INSERT_GREAT_TRICK_HERE("earn"+i+"Tv"));
}
instead of
earnTvs[0] = (TextView) findViewById(R.id.earn1Tv);
earnTvs[1] = (TextView) findViewById(R.id.earn2Tv);
earnTvs[2] = (TextView) findViewById(R.id.earn3Tv);
earnTvs[3] = (TextView) findViewById(R.id.earn4Tv);
earnTvs[4] = (TextView) findViewById(R.id.earn5Tv);
timeTvs[0] = (TextView) findViewById(R.id.time1Tv);
...
ownTvs[0] = (TextView) findViewById(R.id.own1Tv);
...
costTvs[0] = (TextView) findViewById(R.id.build1Tv);
...
buyBtns[0] = (ImageButton) findViewById(R.id.buy1Bt);
...
progressBars[0] = (ProgressBar) findViewById(R.id.prog1pB);
...
buildBtns[0] = (Button) findViewById(R.id.build1Bt);
...
Or is there any kinky trick, that can be used?
解决方案I would do it like that:
final int[] earnTvsId = new int[] {R.id.earn1Tv, R.id.earn2Tv, R.id.earn3Tv, R.id.earn4Tv ...};
for(int i = 0; i < earnTvsId.length; ++i){
earnTvs[i] = (TextView) findViewById(earnTvsId[i]);
}
If you want to use the getIdentifier()
method:
for (int i = 0; i < NUMBER_OF_TEXTVIEWS; ++i) {
final int resId = getResources().getIdentifier("earn" + i + "Tv", "id", getPackageName());
earnTvs[i] = (TextView) findViewById(resId);
}
上一篇:编程的android模糊ImageView的绘制模糊、android、ImageView
下一篇:Android的套接字服务器的DataInputStream着秀再次经过DATAS服务器、Android、DATAS、DataInputStream