专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 移动开发

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

发布时间:2010-05-30 00:04:25 文章来源:www.iduyao.cn 采编人员:星星草
自定义Toast,跟系统的一样好用,一样使用!!!

今天要做一个提示功能,所以看系统的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;
        }
    }

}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: