AndroidではToast表示をカスタマイズする時間が長い.
4661 ワード
// : 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);toast 1=Toast.makeText(MainActivity.this、「表示完了」,0);
}public void click(Viewv){Toast toast=Toast.makeText(MainActivity.this,「10秒表示」,1);showMyToast(toast,10000);Timer timer=new Timer();timer.schedule(new TimerTask(){@Override public void run(){toast 1.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 ); }
}