基于int定义视图

时间:2012-01-28 06:36:00

标签: android view

无论如何都要根据int的值更改您正在编辑的视图?

不使用if语句?

例如:

int counter = 1;

public void attach(){
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

TextView text1 = (TextView) findViewById(R.id.text1)
TextView text2 = (TextView) findViewById(R.id.text2)
TextView text3 = (TextView) findViewById(R.id.text3)
TextView text4 = (TextView) findViewById(R.id.text4)

if (counter < 5){
if (sp.getInt("test" + counter , 0) == counter){

text + counter .setText("test" + counter);

}else{counter = counter + 1; attach();}}

}

1 个答案:

答案 0 :(得分:0)

最简单的方法是定义一个可能的视图ID数组。

int[] viewIds = new int[] {
    R.id.text1,
    R.id.text2,
    R.id.text3,
    R.id.text4
}

//some code
int i = /*whatever*/;
TextView tv = (TextView)findViewById(viewIds[i]);
tv.setText("test"+i);