首页 > 其他 > 详细

自定义Toast,跟系统的一样好用,一样使用!!!

时间:2015-03-25 23:46:49      阅读:502      评论:0      收藏:0      [点我收藏+]

今天要做一个提示功能,所以看系统的Toast代码,自已改装了一下,下面这个就是精简版的Toast,使用方法和系统的方法一样,可以自己定义toast的layout。不多聊了,请看代码!!!

下面是自己写的自定义Toast的代码,

public class MyToast {

    private final static String TAG = "MyToast";
    public final static int HIDETOAST = 0;
    public final static long SHORTTIME = 1000;
    public final static long LONGTIME = 2000;

    private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
    WindowManager mWM;
    
    private View mShowView;
    private long mDuration;
    private Handler     mHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int what = msg.what;
        switch(what){
                 case HIDETOAST:
                hide();
                break;
            default:
                break;
        }
      }
    };
    
    public MyToast(Context context) {
        mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        mParams.format = PixelFormat.TRANSLUCENT;
        mParams.windowAnimations = R.style.mytoast_anim;
        mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        mParams.x = 0;
        int height = mWM.getDefaultDisplay().getHeight();
        mParams.y = -height/2;
    }

    public static MyToast makeText(Context context, CharSequence text, long duration) {
        MyToast result = new MyToast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.mytoast, null);
        TextView tv = (TextView)v.findViewById(R.id.toast_msg);
        tv.setText(text);
        
        result.mShowView = v;
        result.mDuration = duration;

        return result;
    }
    
    public static MyToast makeText(Context context, int resId, long duration)
                                throws Resources.NotFoundException {
        return makeText(context, context.getResources().getText(resId), duration);
    }
    
    public void show(){
       if (mShowView.getParent() != null) {
         Log.i( TAG, "mshowView:"+mShowView+" has parent.");
           mWM.removeView(mShowView);
        }
      //ScaleAnimation animation = new ScaleAnimation(1.0f,1.0f,0.0f,1.0f);
      //animation.setDuration(2000);
      //mShowView.setAnimation(animation);
        mWM.addView(mShowView, mParams);
        mHandler.sendEmptyMessageDelayed(HIDETOAST, mDuration);
    }

    public void hide() {
       if (mShowView != null) {
            if (mShowView.getParent() != null) {
                Log.v(TAG, "REMOVE! " + mShowView + " in " + this);
                mWM.removeView(mShowView);
            }
            mShowView = null;
        }
    }

}

自定义Toast,跟系统的一样好用,一样使用!!!

原文:http://blog.csdn.net/yangzs516/article/details/44627903

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!