android 自定义toast停留时间

Toast类

    //首先定义全局变量mToast已经显示和隐藏函数
    private Toast mToast;
    // 隐藏toast信息框常量
    public static final int AIRPLAY_MESSAGE_HIDE_TOAST = 22;
    // 显示toast信息框时间
    public static final int AIRPLAY_TOAST_DISPLAY_TIME = 1000;

    public void showShortToast(String text) {
        if(mToast == null) {
            mToast = Toast.makeText(AppContext.getInstance(), text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

调用


final Handler m_Handler = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        // TODO Auto-generated method stub
                        switch (msg.what) {
                            case 0:
                                break;
                            case BaseActivity.AIRPLAY_MESSAGE_HIDE_TOAST: {
                                cancelToast();
                                break;
                            }
                        }
                    }
                };
      showShortToast(getString(R.string.error_tag_name_input));
      Message delayMsg = m_Handler.obtainMessage(AIRPLAY_MESSAGE_HIDE_TOAST);
      m_Handler.sendMessageDelayed(delayMsg, AIRPLAY_TOAST_DISPLAY_TIME);

   private static final int LONG_DELAY = 3500// 3.5 seconds           

   private static final int SHORT_DELAY = 2000// 2 seconds


Toast.Long = 3.5s

Toast.short = 2.0s

Toast..makeText( Context  context, CharSequence  text, int duration) .show() 中的duration只能设这两个值,其他值是没有用的

你可能感兴趣的:(android)