我是Java的数组和我们的最终项目的新手,而不是创建3000个活动,我决定使用单个数组来存放我的所有字符串。我现在遇到的问题是,当我按下按钮更改屏幕上的字符串时,它会跳到最后或以某种方式将它们全部添加到一起。我希望它一次只显示一个字符串而不能,因为我的生活想出来了。
这是我的代码:
public class MainActivity extends Activity {
MediaPlayer Snake;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] Lines = {"So begins the story of our hero.","His name is Solid Snake.","He is an international spy, an elite solider, and quite the ladies man.",
"Snake likes to sneak around in his cardboard box.","Most enemies aren't smart enough to catch him in it."};
Snake = MediaPlayer.create(this, R.raw.maintheme);
Snake.start();
final TextView tv = (TextView)findViewById(R.id.textView1);
Button N = (Button)findViewById(R.id.next);
Button B = (Button)findViewById(R.id.back);
int count = 0;
tv.setText(Lines[count]);
N.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String temp = "";
for(int l=1; l<Lines.length; l++){
temp=temp+Lines[l];
tv.setText(""+temp);
}
}
});
};
主要问题在于按下按钮。我到处搜索,根本找不到任何答案。任何帮助将不胜感激。
答案 0 :(得分:0)
单击该按钮时,文本将通过数组中的每个条目进行更改,并在最后一个条目上完成。由于这种情况很快发生,您只能看到最后一个值。
您的onClick()
方法应更改为仅调用setText()
一次并增加活动中保留的计数器。
public class MainActivity extends Activity {
private int currentLine = 0;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
tv.setText(Lines[currentLine]);
N.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (currentLine + 1 < Lines.length) {
currentLine++;
tv.setText(Lines[currentLine]);
}
}
});
...
}
...
}