gpt4 book ai didi

安卓 : How to repeat a Toast every second

转载 作者:搜寻专家 更新时间:2023-11-01 08:50:23 24 4
gpt4 key购买 nike

我正在为倒数计时器使用 toast,所以 toast 应该每秒更改它的文本。我用它来显示 toast 恰好 1 秒,但我希望 toast 自己重复。希望我能让你明白。

 toast =    Toast.makeText(getApplicationContext(), text.getText().toString(), Toast.LENGTH_SHORT);             toast.show();
Handler handler = new Handler();
handler.postDelayed
(new Runnable() {

@Override
public void run() {
toast.cancel();
}
}, 1000);

最佳答案

这将在整整一秒内每秒显示一个新的 toast。

    int count = 100; //Declare as inatance variable

Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
runOnUiThread(new Runnable() {

@Override
public void run() {
final Toast toast = Toast.makeText(
getApplicationContext(), --count + "",
Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {

@Override
public void run() {
toast.cancel();
}
}, 1000);

}
});
}
}, 0, 1000);

关于安卓 : How to repeat a Toast every second,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24007992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com