-1

基本上,该应用程序会打开一个菜单,并在顶部显示 Welcome to bla bla。我想在他们每次打开应用程序时将其更改为不同的内容,以便下次它会说。Bla bla很棒,你也是。下次它可能会显示不同的文本。基本上我希望能够创建一个字符串数组,并且每次用户打开应用程序/活动时,都会选择并显示一个随机字符串。如果有更好的方法通过字符串数组进行,我愿意接受建议。谢谢!!

4

2 回答 2

1

构建一个字符串数组,每次随机选择一个。

例如,这将生成一个介于 0 和 99 之间的随机数:

Random ran = new Random();
randomNum  = ran.nextInt(100);

因此,您可以从数组中调用一个字符串:myArray.get(randomNum)

于 2013-05-07T00:40:57.617 回答
1

您应该使用共享首选项,并且每次用户启动应用程序时都会显示另一句话。

您应该将句子保存在您的共享首选项中,并保存一个计数器来检索您在 str 数组中的索引。

应该是这样的:

    String Sentences = "sen one, sen two, sen three, sen four";
int counter = 0;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("sentences", Sentences);
editor.putInt("counter", counter);
editor.commit();

在这里,您将句子和计数器保存在您的设备中。

现在你如何获得当前句子并显示?应该是这样的:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit()
String sen = prefs.getString("sentences", "");
int counter = prefs.getInt("counter", 0);
String[] fetchArray= sen.split(",");

//now you should display fetchArray[counter].
tv_1.settext(fetchArray[counter] + "");

counter = counter+1;

if (counter == fetchArray[counter].length)
    counter = 0;

editor.putInt("counter", counter);
editor.commit();

这应该完全有效。晚安。

于 2013-05-07T00:46:00.363 回答