内部类的使用toast

前面的话:当一个类中需要用到一个view的类,而这个类只有这个类自己会去调用,别的类不会去调用时,就可以使用内部类的方法,这样可以使的代码高内聚,低耦合,若别人想用到你这个类时,便可以直接复制这个类,而不需要再添加其他的附加类了。
前面所有set方法,都只是改变变量的值,而在show()方法中定义新建一个内部view类的方法并根据参数的值将view显示出来。
如下:

    public TMToast(Context context) {
        if(context != null) {
            mContext = context.getApplicationContext();
            mResources = context.getResources();
            mInflater = LayoutInflater.from(mContext);
        }
    }



    /**
     * 设置toast显示的图标
     * 
     * @param iconDrawable
     */
    @Deprecated
    public void setToastIcon(Drawable iconDrawable) {
        icon = iconDrawable;
    }

    /**
     * 设置toast显示的iconfont图标
     *
     */
    public void setToastIconWithIconfont(String iconfontCode) {
        msgIconfontCode = iconfontCode;
    }

    /**
     * 设置toast显示的图标
     * 
     */
    public void setToastIcon(int iconDrawableResId) {
        setToastIcon(mResources.getDrawable(iconDrawableResId));
    }

    public TMToast setToastGravity(int mToastGravity) {
        this.mToastGravity = mToastGravity;
        return this;
    }

    /**
     * 设置Toast显示的文字.
     * 
     * @param msg
     */
    public void setToastMsg(CharSequence msg) {
        message = getUtilHint((msg == null)?"":msg.toString());
    }

    public String getUtilHint(String message){
        if(message == null){
            return "";
        }
        String RESP_PARSE_ERROR = "Response parsing error";
        String RESP_RMPTY = "Response empty";
        if(message.contains(RESP_RMPTY)
                || message.contains(RESP_PARSE_ERROR)
                ){
            message = mContext.getString(R.string.tm_str_response_parse_err_form_network);
        }
        return message;
    }

    /**
     * 设置Toast显示的文字.
     * 
     */
    public void setToastMsg(int msgResId) {
        setToastMsg(mResources.getString(msgResId));
    }

    /**
     * 设置显示时间 Toast.LENGTH_LONG和Toast.LENGTH_SHORT
     * 
     * @param dura
     */
    public void setDuration(int dura) {
        mDuration = dura;
        if(dura== Toast.LENGTH_LONG)
        {
            mDuration=4000;
        }
        else if(dura==Toast.LENGTH_SHORT)
        {
            mDuration=2000;
        }
    }

    /**
     * 创建Toast对象
     */
    public TMToastView create() {
            if(mContext == null) {
                return null;
            }
            tmToastView= new TMToastView(mContext);

        return tmToastView;
    }


    /**
     * 显示这个TMToast
     */
    public void show() {
        tmToastView=create();
        tmToastView.showAsToast(icon,message,msgIconfontCode,mDuration);
    }


    /**
     * 配置一个TMToast
     *
     * @param context
     * @param iconType
     *            文字左边的图标类型,可能的值为ICON_DEFAULT,ICON_ERROR,ICON_SUCCESS
     * @param msg
     * @param duration
     * @return
     */
    public static TMToast makeText(Context context, int iconType, CharSequence msg, int duration ) {
        TMToast tmToast = new TMToast(context);
        tmToast.setToastIcon(TMWidgetBase.getIconRes(iconType));
        tmToast.setToastMsg(msg);
        tmToast.setDuration(duration);
        return tmToast;
    }


    /**
     * 配置一个TMToast
     * 
     * @param context
     * @param iconType
     *            文字左边的图标类型,可能的值为ICON_DEFAULT,ICON_ERROR,ICON_SUCCESS
     * @param msgResId
     * @param duration
     * @return
     */
    public static TMToast makeText(Context context, int iconType, int msgResId, int duration) {
        TMToast tmToast = new TMToast(context);
        tmToast.setToastIcon(TMWidgetBase.getIconRes(iconType));
        tmToast.setToastMsg(msgResId);
        tmToast.setDuration(duration);
        return tmToast;
    }

    public static TMToast makeTextSuccess(Context context, int msgResId, int duration) {
        return makeText(context, TMWidgetBase.ICON_SUCCESS, msgResId, duration);
    }

    public static TMToast makeTextFailed(Context context, int msgResId, int duration) {
        return makeText(context, TMWidgetBase.ICON_ERROR, msgResId, duration);
    }

    /**
     * 配置一个TMToast(默认图标)
     * 
     * @param context
     * @param msg
     * @param duration
     * @return
     */
    public static TMToast makeText(Context context, CharSequence msg, int duration) {
        return makeText(context, TMWidgetBase.ICON_DEFAULT, msg, duration);
    }

    /**
     * 配置一个TMToast(默认图标)
     *
     * @param context
     * @param msg
     * @param duration
     * @return
     */
    public static TMToast makeText(Context context, CharSequence msg, int duration , String imgIconfont) {
        TMToast tmToast = new TMToast(context);
        tmToast.setToastIconWithIconfont(imgIconfont);
        tmToast.setToastMsg(msg);
        tmToast.setDuration(duration);
        return tmToast;
    }


    /**
     * 配置一个TMToast(默认图标)
     * 
     * @param context
     * @param msgResId
     * @param duration
     * @return
     */
    public static TMToast makeText(Context context, int msgResId, int duration) {
        return makeText(context, TMWidgetBase.ICON_DEFAULT, msgResId, duration);
    }



    public class TMToastView extends LinearLayout {
        private Context mContext;
        private final int MSG_CLOSE_TOASTVIEW = 1;

        private Drawable icon = null;
        private CharSequence message = null;
        private ImageView imageView;
        private TextView toastMessage;
        private String msgIconfontCode;

        private Handler uiHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_CLOSE_TOASTVIEW:
                        dismiss();
                        break;
                }

                super.handleMessage(msg);
            }
        };


        public TMToastView(Context context) {
            super(context);
            mContext = context;
            initView();
        }


        public void setParameters(Drawable icon, CharSequence message, String msgIconfontCode) {
            this.icon = icon;
            this.message = message;
            this.msgIconfontCode = msgIconfontCode;
        }


        public void initView() {
            mLoadingRootView = inflate(getContext(), R.layout.tm_widget_toast_base, this);

            int mY = mContext.getResources().getDimensionPixelSize(R.dimen.TMToast_y_offset);
            mLoadingRootView.setPadding(0,0,0,mY*3);

            //iconfont_toast_icon
            if (!TextUtils.isEmpty(msgIconfontCode)) {
                TMIconFontTextView tvMsgIconfont = (TMIconFontTextView) mLoadingRootView.findViewById(R.id.iconfont_toast_icon);
                tvMsgIconfont.setText(msgIconfontCode);
                tvMsgIconfont.setVisibility(View.VISIBLE);
            } else if (icon != null) {
                imageView = (ImageView) mLoadingRootView.findViewById(R.id.toast_icon);
                imageView.setImageDrawable(icon);
            } else {
                ImageView imageView = (ImageView) mLoadingRootView.findViewById(R.id.toast_icon);
                imageView.setImageResource(R.drawable.tm_dialog_default_icon);
            }

            if (message != null) {
                toastMessage = (TextView) mLoadingRootView.findViewById(R.id.toast_msg);
                toastMessage.setText(message);
            }
        }


        public void showAsToast(Drawable icon,CharSequence msg, String msgIconfontCode,int dura)
        {
            setParameters(icon,msg,msgIconfontCode);
            initView();
            addToWindow();

            if(dura== Toast.LENGTH_LONG)
            {
                dura=4000;
            }
            else if(dura==Toast.LENGTH_SHORT)
            {
                dura=2000;
            }
            dismiss(dura);
        }



        private void dismiss() {
            if(mViewAdded) {
                mLoadingRootView.setVisibility(View.GONE);
                WindowManager localWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                if (localWindowManager != null) {
                    try {
                        localWindowManager.removeViewImmediate(mLoadingRootView);
                        mViewAdded = false;
                    } catch (Exception e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
            }
        }

        private void dismiss(int mDuration) {
            Message msg = uiHandler.obtainMessage();
            msg.what = MSG_CLOSE_TOASTVIEW;
            uiHandler.sendMessageDelayed(msg, mDuration);
        }


        private void addToWindow() {
            try {
                int height = getLoadingHeight();
                int h = height > 0 ? height : ViewGroup.LayoutParams.MATCH_PARENT;
                WindowManager.LayoutParams lp = null;
                lp = new WindowManager.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        h,
                        WindowManager.LayoutParams.TYPE_TOAST,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                        PixelFormat.TRANSLUCENT | WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW);
                WindowManager localWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                lp.gravity = Gravity.BOTTOM;

                // 添加悬浮窗或者更新悬浮窗 如果悬浮窗还没添加则添加 如果已经添加则更新其位置
                if (mViewAdded) {
                    localWindowManager.updateViewLayout(mLoadingRootView, lp);
                } else {
                    mLoadingRootView.setVisibility(View.VISIBLE);
                    localWindowManager.addView(mLoadingRootView, lp);
                    mViewAdded = true;
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }

        private int getLoadingHeight() {
            if (mContext instanceof Activity) {
                Rect frame = new Rect();
                Activity activity = ((Activity) mContext);
                activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

                int statusBarHeight = frame.top;
                if (statusBarHeight == 0) {
                    statusBarHeight = getStatusBarHeight();
                }

                if (activity.getActionBar() != null) {
                    int actionBarHeight = activity.getActionBar().getHeight();
                    if (actionBarHeight == 0) {
                        actionBarHeight = getActionBarHeight();
                    }
                    return frame.bottom - statusBarHeight - actionBarHeight;
                } else {
                    return frame.bottom - statusBarHeight;
                }
            }

            return 0;
        }

        private int getActionBarHeight() {
            int actionBarHeight = 0;
            TypedValue tv = new TypedValue();
            if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(
                        tv.data, mContext.getResources().getDisplayMetrics());
            }

            return actionBarHeight;
        }

        private int getStatusBarHeight() {
            int statusBarHeight = 0;
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object obj = c.newInstance();
                Field field = c.getField("status_bar_height");
                int dpSize = Integer.parseInt(field.get(obj).toString());
                statusBarHeight = mContext.getResources().getDimensionPixelSize(dpSize);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return statusBarHeight;
        }

    }




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值