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

ConstraintLayout实现的效果
均分布局

<ImageView
        android:id="@+id/iv_divide"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="55dp"
        android:background="@color/gray_linebg"/>
    <ImageView
        android:layout_width="1dp"
        android:layout_height="55dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="@color/gray_linebg"/>
    <TextView
        android:id="@+id/tv_not_agree"
        android:layout_width="0dp"
        android:layout_height="55dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tv_agree"
        android:gravity="center"
        android:textSize="@dimen/sp_16"
        android:text="不同意"/>
    <TextView
        android:id="@+id/tv_agree"
        android:layout_width="0dp"
        android:layout_height="55dp"
        android:gravity="center"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/tv_not_agree"
        android:text="同意"
        android:textSize="@dimen/sp_16"
        android:textColor="@color/red_dot"/>

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();
    }

}

发布了316 篇原创文章 · 获赞 63 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/ytfunnysite/article/details/101760365