Android隱私彈框功能的實現

編寫Android的APP進去點擊的用戶隱私彈框

因為現在發佈APP現在都需要在發佈前生成一個用戶隱私彈框讓用戶點擊,所以手寫瞭一個用以使用

效果圖

Alt

java代碼

public class PrivateDialog {

    private static PrivateDialog instace;

    public PrivateDialog() {

    }

    public static PrivateDialog getInstace() {

        if (instace == null) {
            synchronized (PrivateDialog.class) {
                if (instace == null) {
                    instace = new PrivateDialog();
                }
            }
        }
        return instace;
    }


    private String title = "溫馨提示";
    private String message;
    private String sure;
    private String cancle;


    private Dialog tipDialog;

    private float clickTiem = 0;


    /**
     * desc: 提示隱私協議框
     */
    public void showConnectDialog(Context mContext) {
        dismiss();
        if ((SystemClock.elapsedRealtime() - clickTiem) < 500) {
            return;
        }
        tipDialog = new AlertDialog.Builder(mContext).create();

        tipDialog.setCanceledOnTouchOutside(false);

        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_private_dialog, null, false);
        TextView tvTitle = view.findViewById(R.id.tv_sava_dialog_title);
        TextView dialogTxt = view.findViewById(R.id.tv_sava_dialog_message);
        TextView tvcancle = view.findViewById(R.id.tv_sava_dialog_cancel);
        TextView tvsure = view.findViewById(R.id.tv_sava_dialog_confirg);
        SpannableStringBuilder tvProtocol = new SpannableStringBuilder(dialogTxt.getText().toString());
        tvProtocol.setSpan(new CliclSpan(mContext,1),27,35,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvProtocol.setSpan(new CliclSpan(mContext,2),36,42,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        dialogTxt.setText(tvProtocol);
        dialogTxt.setMovementMethod(LinkMovementMethod.getInstance());


        if (!this.title.isEmpty()) {
            tvTitle.setText(this.title);
        }
        if (!this.message.isEmpty()) {
//            tvMsg.setText(this.message);
        }
        if (!this.cancle.isEmpty()) {
            tvcancle.setText(this.cancle);
        }
        if (!this.sure.isEmpty()) {
            tvsure.setText(this.sure);
        }


        view.findViewById(R.id.tv_sava_dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickTiem = SystemClock.elapsedRealtime();
                tipDialog.dismiss();
                if (listener != null) {
                    listener.cancleClick();
                }
            }
        });

        tvsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tipDialog.dismiss();
                if (listener != null) {
                    listener.sureClick();
                }

            }
        });
        tipDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_SEARCH)
                {
                    return true;
                }
                else
                {
                    return false; //默認返回 false
                }
            }
        });
        tipDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        tipDialog.show();
        tipDialog.setCancelable(false);
        tipDialog.getWindow().setContentView(view);
        //tipDialog.getWindow().setWindowAnimations(R.style.DialogBottom); // 添加動畫

        WindowManager windowManager = (WindowManager)
                mContext.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams params = tipDialog.getWindow().getAttributes();
        params.width = (int) (display.getWidth() * 0.8);
        tipDialog.getWindow().setAttributes(params);

    }

    public void dismiss() {
        if (tipDialog != null) {
            if (tipDialog.isShowing()) {
                tipDialog.dismiss();
            }
            tipDialog = null;
        }
    }

    public interface OnTipItemClickListener {
        void cancleClick();

        void sureClick();

        void userClick();

        void termsClick();
    }

    private OnTipItemClickListener listener;

    public PrivateDialog setOnTipItemClickListener(OnTipItemClickListener listener) {
        this.listener = listener;
        return this;
    }


    public PrivateDialog title(String title) {
        this.title = title;
        return this;
    }

    public PrivateDialog message(String message) {
        this.message = message;
        return this;
    }

    public PrivateDialog sure(String sure) {
        this.sure = sure;
        return this;
    }

    public PrivateDialog cancle(String cancle) {
        this.cancle = cancle;
        return this;
    }


    public void create(Context mContext) {
        showConnectDialog(mContext);

    }
    class CliclSpan extends ClickableSpan{
        Context mContext;
        Integer mNum;
        public CliclSpan() {
            super();
        }
        public CliclSpan(Context context,Integer num) {
            mContext = context;
            mNum = num;
        }
        @Override
        public void onClick(@NonNull View widget) {
            if (listener != null) {
                if(mNum == 1){
                    listener.userClick();
                }else {
                    listener.termsClick();
                }
            }
        }

        @Override
        public void updateDrawState(@NonNull TextPaint ds) {
            ds.setColor(ContextCompat.getColor(mContext,R.color.red));
            ds.setUnderlineText(false);
        }
    }

}

xml佈局界面

xml文件名:activity_private_dialog

<?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="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_sava_dialog_title"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="@string/tip_title"
            android:textColor="#bababa"
            android:textSize="17sp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/ll_read_priavate"
                android:paddingLeft="@dimen/dp_20"
                android:paddingRight="@dimen/dp_20">

                <TextView
                    android:id="@+id/tv_sava_dialog_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="歡迎使用XXXXXXXXXX!在您使用前,請您認真閱讀《用戶服務協議》和《隱私政策》,以瞭解我們的服務內容和我們在收集和使用您相關個人信息時的處理規則"
                    android:textColor="#bababa"
                    android:layout_marginBottom="18dp"
                    android:textSize="@dimen/sp_15" />
            </LinearLayout>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            >

            <TextView
                android:id="@+id/tv_sava_dialog_cancel"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#a1a1a1"
                android:clickable="true"
                android:foreground="?android:selectableItemBackground"
                android:gravity="center"
                android:text="@string/cancel"
                android:textColor="#867575"
                android:textSize="15sp" />


            <TextView
                android:id="@+id/tv_sava_dialog_confirg"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginLeft="1dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#a1a1a1"
                android:clickable="true"
                android:foreground="?android:selectableItemBackground"
                android:gravity="center"
                android:text="@string/confirm"
                android:textColor="#efebeb"
                android:textSize="15sp" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

如何使用

服務協議與隱私政策的Activity我就不貼出來瞭,有需要可以找我要

PrivateDialog.getInstace().message("").sure("同意").cancle("退出應用")
                    .setOnTipItemClickListener(new PrivateDialog.OnTipItemClickListener() {
                        @Override
                        public void cancleClick() {
                            finishAffinity();
                        }

                        @Override
                        public void sureClick() {
							//進入app
                        }

                        @Override
                        public void userClick() {
							//跳轉至用戶服務協議
                            toActivity(ProtocolActivity.class);
                        }

                        @Override
                        public void termsClick() {![在這裡插入圖片描述](https://img-blog.csdnimg.cn/23138f9cd70548c2b8eb36547a10fc98.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5pep5pma6YGH6KeB5L2g,size_8,color_FFFFFF,t_70,g_se,x_16#pic_center)

							//跳轉至隱私政策
                            toActivity(TermActivity.class);
                        }
                    }).create(this);

到此這篇關於Android隱私彈框的文章就介紹到這瞭,更多相關Android隱私彈框內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: