美文网首页
PopUtil 顶部弹出提示--PopupWindow

PopUtil 顶部弹出提示--PopupWindow

作者: 颤抖的闪电 | 来源:发表于2018-10-31 20:31 被阅读0次

    效果如下:


    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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="#55cf7d"
        android:fitsSystemWindows="true"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/txtToastMessage"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="测试测试"
                android:textColor="#ffffff"
                android:textSize="18sp" />
    
            <TextView
                android:id="@+id/txt_change_account"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:gravity="center"
                android:text="切换账号"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        </LinearLayout>
    
    </LinearLayout>
    

    调用如下:

    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

          本文链接:https://www.haomeiwen.com/subject/hfrztqtx.html