Android中自定義Toast顯示的時長.


//注意:該方法創建Toast對象的時候時長因該設置為 Toast.LENGTH_LONG,因為該他的時長就是3秒,與下面的延時時間對應
//cnt:需要顯示的時長,毫秒
private void showMyToast(final Toast toast, final int cnt) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
}, 0, 3000);//每隔三秒調用一次show方法;
 
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, cnt );//經過多長時間關閉該任務
}
 
-----------------------------------------------------------華麗的分割線---------------------------------------------------------
 
Demo:

public class MainActivity extends Activity {

private Toast toast1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toast1 = Toast.makeText(MainActivity.this, "顯示完成了", 0);

}


public void click(View v)
{
Toast toast = Toast.makeText(MainActivity.this, "顯示十秒鍾", 1);
showMyToast(toast, 10000);

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast1.show();
}
}, 10000);

}

private void showMyToast(final Toast toast, final int cnt) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
}, 0, 3000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, cnt );
}

}

 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM