您好,登錄后才能下訂單哦!
背景:
缺省狀態(tài)下,Toast顯示時(shí)間大約在1~2秒時(shí)間,有時(shí)需要讓彈出窗顯示更長(zhǎng)的時(shí)間。
案例:
可通過(guò)調(diào)用CountDownTimeer例來(lái)達(dá)到此目標(biāo)。
public class ToastActivity extends Activity
{
AlertDialog dialog;
static CountDownTimer timer =null;
Toast toast;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
toast = new Toast(this);
TextView textView=new TextView(this);
textView.setTextColor(Color.BLUE);
textView.setBackgroundColor(Color.TRANSPARENT);
textView.setTextSize(20);
textView.setText("This Toast will Display for 20 Seconds in Center of The Screen");
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(textView);
// Create the CountDownTimer object and implement the 2 methods
// show the toast in onTick() method and cancel the toast in onFinish() method
// it will show the toast for 20 seconds (20000 milliseconds 1st argument) with interval of 1 second(2nd argument)
timer =new CountDownTimer(20000, 1000)
{
public void onTick(long millisUntilFinished)
{
toast.show();
}
public void onFinish()
{
toast.cancel();
}
}.start();
}
}
通過(guò)調(diào)用timer.cancel()可以取消Toast的顯示。
結(jié)果:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。