How to set Toast display time less than Toast.LENGTH_SHORT

北城余情 提交于 2019-12-17 22:58:59

问题


I want to display toast less than Toast.LENGTH_SHORT, as i feel its taking around 2 seconds. i want to display toast only for half second.

And what is time interval for Toast.LENGTH_SHORT and Toast.LENGTH_LONG ?


回答1:


There are only two possible values:

private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds

Setting other values doesn't work. If duration not equals 1 (Toast.LENGTH_LONG), then duration will be SHORT_DELAY (2 seconds):

long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);

In sources of Toast written that

This time could be user-definable.

but I can't find way to do this.

Update: There is solution here: Set Toast Appear Length




回答2:


This has worked for me

final Toast toast = Toast.makeText(getApplicationContext(), "The following message will disappear in half second", Toast.LENGTH_SHORT);
    toast.show();

    Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               toast.cancel(); 
           }
    }, 500);



回答3:


See my suggested solution here. You basically call toast.cancel() after a specified delay that is shorter than the standard toast duration.




回答4:


Try this

final Toast toast = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);
            toast.show();
            new CountDownTimer(10000, 1000)
            {
                public void onTick(long millisUntilFinished) {toast.show();}
                public void onFinish() {toast.cancel();}
            }.start();

Hope this help.. Enjoy..!!!




回答5:


For noobs I've made the simplest solution - a method.

public void showToastMessage(String text, int duration){
        final Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
        toast.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
}

You should also import:

import android.os.Handler;
import android.widget.Toast;

You can call this method for example:

showToastMessage("your noob", 1000);

method above should work only in Fragment! If you want it to work in Activity, replace getActivity() with getApplicationContext() in toast message. Good luck developers!




回答6:


it will work.


   public void toastMessage(final String message) {
    this.runOnUiThread(new Runnable() {
        public void run() {
            LayoutInflater myInflator = getLayoutInflater();
            View myLayout = myInflator.inflate(R.layout.custom_layout,
                    (ViewGroup) findViewById(R.id.toastlayout));
            TextView myMessage = (TextView) myLayout
                    .findViewById(R.id.label);
            myMessage.setText(message);
            Toast toast = new Toast(getApplicationContext());
            toast.setView(myLayout);
            toast.setDuration(100);
            myMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL
                    | Gravity.CENTER_VERTICAL);
            toast.show();
        }
    });
}



回答7:


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

//how to use(MainActivity.this is example ,it could be changed by yourself) and (In showMyToast(first param ,second param) method, second parameter is time what u really defined like this)

Toast toast=Toast.makeText(MainActivity.this, "MyToast Test", Toast.LENGTH_LONG);
            showMyToast(toast, 1000);



回答8:


This seems to work for me (set the duration to whatever you want):

mActivity.runOnUiThread(new Runnable() {
                    public void run() {
                        Toast toast = new Toast(mActivity
                                .getApplicationContext());
                        toast.setView(layout);
                        toast.setDuration(400);
                        toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
                        toast.show();
                    }
                });


来源:https://stackoverflow.com/questions/6094792/how-to-set-toast-display-time-less-than-toast-length-short

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!