大家好,我是相当新的,可能需要一些帮助,这很简单,
我需要为此代码添加什么
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv2 = (TextView) findViewById(R.id.textView2);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView2);
tx.setText("Hello");
}
});
}
}
按下按钮时,它会将文本视图更改为strings.xml列表中的随机字符串 如果我标记了字符串,string1,string2等
答案 0 :(得分:1)
为什么不创建string-array
并使用它,如下所示:
尝试这种方式:
Resources res = this.getResources(); final String[] capital = res.getStringArray(R.array.list); Button next=(Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(count<capital.length-1) tv2.setText(capital[count]); count++; } });
答案 1 :(得分:0)
public class main extends Activity {
/** Called when the activity is first created. */
String[] myStrings;
int currentText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv2 = (TextView) findViewById(R.id.textView2);
Button b = (Button) findViewById(R.id.button1);
currentText = 0;
setListeners();
myStrings = new String [200];
myStrings[0] = "String1";
myStrings[1] = "String2";
}
public void changeText(){
tv2.setText(myStrings[currentText[);
currentText++;
}
public void setListeners(){
b = (Button) findViewById(R.id.textView2);
b.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
changeText();
return false;
}
});
}
}
我没有测试这段代码。我希望这可以帮助你!