ConstraintLayout均分布局和SpannableStringBuilder链接超文本定义用户隐私协议弹窗

ConstraintLayout实现的效果
ConstraintLayout均分布局和SpannableStringBuilder链接超文本定义用户隐私协议弹窗_第1张图片


    
    
    

SpannableStringBuilder 处理隐私协议弹窗文本中的超链接和点击事件

package com.thesis.mentor.base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.thesis.mentor.R;
import com.thesis.mentor.home.HomeActivity1;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import bang.lib.utils.SPUtils;
import bang.lib.utils.ToastUtils;

/**
 * @author YangTianFu
 * @date 2019/9/12  14:36
 * @description
 */
public class UserAgreementDialog {
    private IUserRefuse userRefuse;
    private Dialog dialog;
    public void setUserRefuse(IUserRefuse userRefuse) {
        this.userRefuse = userRefuse;
    }

    public UserAgreementDialog() {
    }

    public static UserAgreementDialog getInstance() {
        return UserAgreementDialogHolder.instance;
    }

    public void showDialog(Context context){
        dialog=getDialog(context);
        dialog.setContentView(R.layout.user_agreement);
        dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        if (context!=null){
            if (dialog.isShowing()){
                dialog.cancel();
            }
            dialog.show();
            SPUtils.WritBoolean(context,"isFirstLaunch","isFirstLaunch",true);
            dialogWindow.setAttributes(p);
            TextView textView = dialog.findViewById(R.id.tv_tips);
            final SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
            int index = Constants.user_agreement.indexOf("《隐私政策》");
            stringBuilder.append(Constants.user_agreement);
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {
                // 要跳转的链接
                    Uri uri = Uri.parse("https://www.8888.com/privacy_agreement");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    context.startActivity(intent);
                }

                @Override
                public void updateDrawState( @NonNull TextPaint ds) {
                    ds.setColor(ds.linkColor);
                    ds.setUnderlineText(false);
                }
            };
            stringBuilder.setSpan(clickableSpan,index,index+10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(stringBuilder);

            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#ff0000"));
            stringBuilder.setSpan(foregroundColorSpan,index,index+10,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(stringBuilder);

            textView.setMovementMethod(LinkMovementMethod.getInstance());
            textView.setText(stringBuilder);

            dialog.findViewById(R.id.tv_not_agree).setOnClickListener(v -> {
                    SPUtils.WritBoolean(context,"isAgree","isAgree",false);

                dialog.cancel();
                if (userRefuse!=null){
                    userRefuse.userRefuse();
                }

            });
            dialog.findViewById(R.id.tv_agree).setOnClickListener(v -> {
                    SPUtils.WritBoolean(context,"isAgree","isAgree",true);
                dialog.cancel();
                dialog = null;
            });
        }

    }

    public Dialog getDialog(Context context) {
        if (dialog == null){
            dialog = new Dialog(context);
        }
        return dialog;
    }

    private static  class UserAgreementDialogHolder{
        private static final  UserAgreementDialog instance = new UserAgreementDialog();
    }

    /**
     * 用户是否同意用户隐私协议的回调
     */
    public interface IUserRefuse{
        void userRefuse();
    }

}

你可能感兴趣的:(Android,用户隐私协议弹窗)