Toast Dialog的封装(二)

text.setText(tvString);
        mHandler.removeCallbacks(r);
        if (toast == null) {//只有mToast==null时才重新创建,否则只需更改提示文字
            toast = new Toast(context);
            toast.setGravity(Gravity.BOTTOM, 0, 100);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
        }
        mHandler.postDelayed(r, 1000);//延迟1秒隐藏toast
        toast.show();
    }
}

写好这个类,在代码中调用吧(就是这么简单)
ToastCommon.toastShortShow(context, null, "打开困难登录界面");



简单吧,下面来封装下Dialog,每次我们新建dialog是不是有很多重复代码,系统自带有时还不能满足我们,继续,上代码—> 封装Dialog(我简单的写了一下,只有两种,同学可自行添加)

/**
 * Created by pc-004 on 2015/8/14.
 */
public abstract class DialogCommon {
    /**
     * 系统自带的Dialog
     *
     * @param context
     * @param icon
     * @param title
     * @param message
     * @param positiveBtnText
     * @param negativeButtonBtnText
     */
    public void dialogShowNavite(Context context, int icon, String title, String message, String positiveBtnText, String negativeButtonBtnText) {
        //先new出一个监听器,设置好监听
        DialogInterface.OnClickListener dialogOnclicListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case Dialog.BUTTON_POSITIVE:
                        positiveClick();
                        dialog.dismiss();
                        break;
                    case Dialog.BUTTON_NEGATIVE:
                        negativeClick();
                        dialog.dismiss();
                        break;
//                    case Dialog.BUTTON_NEUTRAL:
//                        Toast.makeText(MainActivity.this, "忽略" + which, Toast.LENGTH_SHORT).show();
//                        break;
                }
            }
        };
        //dialog参数设置
        AlertDialog.Builder builder = new AlertDialog.Builder(context);  //先得到构造器
        builder.setTitle(title); //设置标题
        builder.setMessage(message); //设置内容
        builder.setIcon(icon);//设置图标,图片id即可
        builder.setPositiveButton(positiveBtnText, dialogOnclicListener);

        builder.setNegativeButton(negativeButtonBtnText, dialogOnclicListener);
//        builder.setNeutralButton("忽略", dialogOnclicListener);
        builder.create().show();
    }

    /**
     * 自定义dialog
     *
     * @param context
     * @param viewGroup
     * @param leftBtn
     * @param rightBtn
     * @param title
     */
    public void dialogShowDiy(Context context, ViewGroup viewGroup, String leftBtn, String rightBtn, String title) {

        final Dialog dialog = new AlertDialog.Builder(context).create();
        dialog.setContentView(viewGroup);
        final TextView tv_left_bottom_dialog = (TextView) viewGroup.findViewById(R.id.tv_left_bottom_dialog);
        final TextView tv_right_bottom_dialog = (TextView) viewGroup.findViewById(R.id.tv_right_bottom_dialog);
        final TextView tv_title_dialog = (TextView) viewGroup.findViewById(R.id.tv_title_dialog);

        tv_left_bottom_dialog.setText("登录");
        tv_right_bottom_dialog.setText("重新输入");
        tv_title_dialog.setText("此号码已注册,是否立即登录?");
        tv_left_bottom_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                leftBtnClick();
                dialog.cancel();
            }
        });

        tv_right_bottom_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightBtnClick();
                dialog.cancel();
            }
        });
        dialog.show();
    }

    /**
     * 原生的dialog
     */
    public abstract void positiveClick();

    public abstract void negativeClick();

    /**
     * 自定义dialog
     */
    public abstract void leftBtnClick();

    public abstract void rightBtnClick();
}
好了,开始在代码中使用吧

    /**
     * 显示提示窗
     */
    private void dialogShow() {
        DialogCommon dialogCommon = new DialogCommon() {
            @Override
            public void positiveClick() {
                LoginActivity.handler.sendEmptyMessage(1);
            }

            @Override
            public void negativeClick() {

            }

            @Override
            public void leftBtnClick() {

            }

            @Override
            public void rightBtnClick() {

            }


        };
        dialogCommon.dialogShowNavite(context, R.mipmap.ic_launcher, getString(R.string.hint),
                "超过尝试次数,请使用验证码登录", getString(R.string.sure),
                getString(R.string.cancle));
    }


    /**
     * 展现自定义dialog
     */
    private void showDialog() {
        DialogCommon dialogCommon = new DialogCommon() {
            @Override
            public void positiveClick() {

            }

            @Override
            public void negativeClick() {

            }

            @Override
            public void leftBtnClick() {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
            }

            @Override
            public void rightBtnClick() {
                etxtTel.setFocusable(true);
                etxtTel.setFocusableInTouchMode(true);
                etxtTel.requestFocus();
                etxtTel.requestFocusFromTouch();
                //调用系统软键盘
                InputMethodManager inputManager = (InputMethodManager)
                        etxtTel.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etxtTel, 0);
            }
        };
        LayoutInflater inflater = LayoutInflater.from(this);
        RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.dialog_customer, null);
        dialogCommon.dialogShowDiy(RegisterActivity.this, relativeLayout, "登录", "重新输入", "此号码已注册,是否立即登录?");
    }
是不是很简单,布局我就不给大家发了,有什么问题,欢迎留言

你可能感兴趣的:(Toast Dialog的封装(二))