歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android延長Toast顯示時間的方法

Android延長Toast顯示時間的方法

日期:2017/3/1 10:50:52   编辑:Linux編程

invokeLongTimeToast()函數關鍵在於調用initToast()方法。而initToast()又會調用execToast()方法,從而引發遞歸,cnt是序列號,當cnt等於3時停止遞歸,用它來調節Toast的顯示時間。

/**
* After a time show a <code>Toast</code> again.
*
* @param toast
* <code>Toast</code>
* @param cnt
* Sequence
*/

private void execToast(final Toast toast, final int cnt) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
initToast(toast, cnt + 1);
}

}, 3000);
}

/**
* Show the <code>Toast</code> and {#execToast}
*
* @param toast
* <code>Toast</code>
* @param cnt
* Sequence
*/
private void initToast(Toast toast, int cnt) {
if (cnt > 2)
return;
toast.show();
execToast(toast, cnt);
}

/**
* Show a <code>Toast</code> much longer than normal.
*
* @param info
* <code>String</code> that wants to show.
*/
public void invokeLongTimeToast(final String info) {
Toast toast = Toast.makeText(sa, info, Toast.LENGTH_LONG);
initToast(toast, 0);

}

Copyright © Linux教程網 All Rights Reserved