我正在做一个假的加载屏幕类型的程序。我希望它显示 1% 然后等待 3 秒然后显示 15% 然后等待 3 秒然后显示 25% 等等。我可以这样设置文本:
prTextView.setText("1%");
我将如何在java中做到这一点?我找到了多个教程,但他们只做了一次而不是多次。
您可以使用CountDownTimer. 检查这个https://developer.android.com/reference/android/os/CountDownTimer#java
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText(100 - (millisUntilFinished / 1000) + "%");
}
public void onFinish() {
mTextField.setText("100%");
}
}.start();
您可以使用递归(一个调用自身的函数)来循环某些功能。使用 Handler 类等待。
void updateProgress(){
progress++;
Handler(Looper.getMainLooper()).postDelayed({
if(progress < 100)
updateProgress();
},3000L);
}
检查这个Runnable thread。
private void fakeProgress() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
final String[] status = {"1%","15%", "25%", "35%", "49%", "60%", "79%", "100%"};
int arraySize = status.length;
for (int i = 0; i < arraySize; i++) {
Log.d("ZI", status[i]);
String status_part = status[i];
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
((TextView) findViewById(R.id.tv)).setText("........" + status_part);
}
});
}
}
};
new Thread(runnable).start();
}