Android 如何在启动屏幕上添加可更改的文本?
嗨,我想在启动屏幕上的启动图像上添加文本。我在图像上添加了单个文本,但我想在每次应用打开时更改文本。如何操作,请告诉我 我在splash.xml中添加了一个文本视图,它正在显示。但我想在每次应用打开时逐个显示list.xml中的所有项目Android 如何在启动屏幕上添加可更改的文本?,android,android-layout,splash-screen,Android,Android Layout,Splash Screen,嗨,我想在启动屏幕上的启动图像上添加文本。我在图像上添加了单个文本,但我想在每次应用打开时更改文本。如何操作,请告诉我 我在splash.xml中添加了一个文本视图,它正在显示。但我想在每次应用打开时逐个显示list.xml中的所有项目 <TextView android:id="@+id/quote" android:layout_width="wrap_content" android:layout_height="wrap_content" andro
<TextView
android:id="@+id/quote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your limitation—it's only your imagination."
android:textColor="#ffffff"
android:textSize="36sp"
android:layout_centerInParent="true"/>
在list.xml中
<string-array name="quote_array">
<item>Your limitation—it's only your imagination.</item>
<item>Great things never come from comfort zones.</item>
<item>The harder you work for something, the greater you’ll feel when you achieve it.</item>
</string-array>
你的局限只是你的想象力。
伟大的事物从不来自舒适区。
你为某件事越努力,当你实现它时,你的感觉就越强烈。
生成一个随机数并从此随机位置的数组中获取一项
示例代码:
TextView quote = findViewById(R.id.quote);
String[] quoteArray = getResources().getStringArray(R.array.quote_array);
int randomPosition = (int)(Math.random()*(quoteArray.length-1))
quote.setText(quoteArray[randomPosition]);
您可以执行以下操作:
public class MyAsyncTask extends AsyncTask<Void, String, Void> {
private String[] stringArray;
private TextView myTextView;
private long delay;
MyAsyncTask(String[] stringArray, long delay, TextView myTextView) {
this.stringArray = stringArray;
this.delay = delay;
this.myTextView = myTextView;
}
@Override
protected Void doInBackground(Void... voids) {
try {
for (String value : stringArray) {
Thread.sleep(delay);
publishProgress(value);
}
} catch (InterruptedException ex) {
// Handle interrupted exception
}
return null;
}
@Override
protected void onProgressUpdate(String... value) {
myTextView.setText(value[0]);
}
}
我希望这对你有帮助。这取决于你想要实现什么。如果顺序无关紧要,您可以通过
new random.nextInt(stringArray.length()-1)使用随机整数代码>每次启动应用程序时。但是,如果要按从0到最后一个的顺序显示字符串,则需要在任何存储中保留上一个索引。我认为SharedReferences
是更好的解决方案
祝你好运 只需在初始屏幕中获取数组列表,然后根据数组大小生成一个随机数,然后设置文本,如arraylist.get(随机数).Quote.settext(arraylist.get(随机数));
TextView myTextView = findViewById(R.id.myTextView);
new MyAsyncTask(
getResources().getStringArray(R.array.quote_array),
5000,
myTextView).execute();