【发布时间】:2015-04-13 13:12:27
【问题描述】:
我想在循环中按下按钮时在 3 个不同的字符串之间切换。
我已将文本放入字符串数组中,但我不确定使用什么来循环数组。
Button ranFw = (Button) findViewById(R.id.ranButFw);
final TextView ranTx = (TextView) findViewById(R.id.ranText);
String[] rangeTx = new String[2];
rangeTx[0]="0 to 1700C";
rangeTx[1]="32 to 3218F";
rangeTx[2]="273.15 to 1973.15K";
ranFw.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
谢谢史蒂夫
更新:
ranFw = (Button) findViewById(R.id.ranButFw); final TextView ranTx = (TextView) findViewById(R.id.ranText);
final String[] rangeTx = new String[3];//String[2] means index from 0 to 1
rangeTx[0] = "0 to 1700C";
rangeTx[1] = "32 to 3218F";
rangeTx[2] = "273.15 to 1973.15K";
ranFw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (index) {
case 0:
// if we are using index 0, set the text to index 1 text and change index to 1
index = 1;
ranTx.setText(rangeTx[index]);
break;
case 1:
index = 2;
ranTx.setText(rangeTx[index]);
break;
case 2:
index = 0;
ranTx.setText(rangeTx[index]);
break;
}
}
});
没有错误,但似乎工作正常
【问题讨论】:
-
index对你来说是什么?