PopUtil 顶部弹出提示--PopupWindow

效果如下:


image.png
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jiyou.jysdklib.R;

public class PopUtil extends PopupWindow {

    private Activity activity;
    private View mPopWindow;
    public TextView txtToastMessage, txt_change_account;
    CountDownTimer countDownTimer;

    public interface PopOnCall {
        void onClick(View v);

        void onFinish();
    }

    public PopUtil(Activity activity) {
        this.activity = activity;
//        LayoutInflater inflater = LayoutInflater.from(activity);
//        mPopWindow = inflater.inflate(R.layout.login_pop_toast, null);//使用LoginActivity加载不到布局资源
        mPopWindow = LayoutInflater.from(activity).inflate(R.layout.jy_login_pop_toast, null);
        txtToastMessage = (TextView) mPopWindow.findViewById(R.id.txtToastMessage);
        txt_change_account = (TextView) mPopWindow.findViewById(R.id.txt_change_account);
        setmPopWindow();
    }

    private void setmPopWindow() {
        // 把View添加到PopWindow中
        this.setContentView(mPopWindow);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(dip2px(activity, 60));
        //  设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        //   设置背景透明
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));
    }

    public static PopUtil get(Activity activity) {
        PopUtil popUtil = new PopUtil(activity);
        return popUtil;
    }

    /**
     * @param str
     * @return
     */
    public PopUtil showNoButton(String str) {
        txtToastMessage.setText(str);
        txt_change_account.setVisibility(View.GONE);
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                showAtLocation(activity.getWindow().getDecorView(),
                        Gravity.TOP, 0, 0);
            }

            @Override
            public void onFinish() {
                if (isShowing()) {
                    dismiss();
                }
            }
        }.start();
        return this;
    }

    /**
     * @param str
     * @param popOnClick
     * @return
     */
    public PopUtil showHasButton(String str, final PopOnCall popOnClick) {
        txtToastMessage.setText(str);
        txt_change_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popOnClick != null) {
                    popOnClick.onClick(v);
                }
            }
        });
        countDownTimer = new CountDownTimer(6000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
//                if (isShowing()) {
                showAtLocation(activity.getWindow().getDecorView(),
                        Gravity.TOP, 0, 0);
//                }
            }

            @Override
            public void onFinish() {
                if (isShowing()) {
                    dismiss();
                    if (popOnClick != null) {
                        popOnClick.onFinish();
                    }
                }
            }
        };
        countDownTimer.start();
        return this;
    }

    @Override
    public void dismiss() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        super.dismiss();
    }


    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

}

布局文件jy_login_pop_toast.xml




    

        

        
    


调用如下:

PopUtil.get((Activity) context).showNoButton(result.getData().getMobile_phone() + "欢迎进入游戏");
final PopUtil popUtil = PopUtil.get((Activity) context);
popUtil.showHasButton(result.getData().getMobile_phone() + "欢迎进入游戏",
                            new PopUtil.PopOnClick() {
                                @Override
                                public void onClick(View v) {
                                   //todo
                                    popUtil.dismiss();
                                }
                            });

感谢:
Android顶部弹出提示的两种实现方式

你可能感兴趣的:(PopUtil 顶部弹出提示--PopupWindow)