Android 实现一个隐私弹窗

效果图如下:

  1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数

  2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来

res/layout/dialog_privacy_policy.xml 文件

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialogRoot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_dialog_rounded"
    android:orientation="vertical"
    android:padding="24dp">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户协议和隐私政策"
        android:textColor="#222222"
        android:textSize="18sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#444444"
        android:textSize="15sp"
        android:lineSpacingExtra="4dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="24dp"
        android:gravity="center">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btnExit"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="退出应用"
            android:textColor="#5E5C3F"
            android:background="@drawable/bg_button_outline"
            android:textSize="16sp" />

        <View
            android:layout_width="16dp"
            android:layout_height="0dp" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btnAgree"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_weight="1.5"
            android:text="已阅读并同意"
            android:textColor="#FFFFFF"
            android:background="@drawable/bg_button_primary"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

res/drawable/bg_dialog_rounded.xml 文件

XML 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF"/>
    <corners android:radius="14dp"/>
</shape>

res/drawable/bg_button_outline.xml文件

XML 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFFFF" />
    <stroke android:width="0.5dp" android:color="#5E5C3F" />
    <corners android:radius="8dp" />
</shape>

res/drawable/bg_button_primary.xml文件

XML 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#5E5C3F" />
    <corners android:radius="8dp" />
</shape>
复制代码
PrivacyPolicyDialog.kt 文件
Kotlin 复制代码
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AlertDialog

class PrivacyPolicyDialog(
    private val context: Context,
    private val onAgree: () -> Unit,
    private val onExit: () -> Unit,
    private val onClickUserAgreement: () -> Unit,
    private val onClickPrivacyPolicy: () -> Unit
) {
    fun show() {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_privacy_policy, null)
        val tvContent = view.findViewById<TextView>(R.id.tvContent)
        val tvTitle = view.findViewById<TextView>(R.id.tvTitle)
        val btnAgree = view.findViewById<Button>(R.id.btnAgree)
        val btnExit = view.findViewById<Button>(R.id.btnExit)

        val content = "在您使用本应用之前,请您务必审慎阅读、充分理解"用户协议"和"隐私政策"各条款内容。详细资料请阅读:《用户协议》和《隐私政策》。"
        val spannable = SpannableString(content)

        val userStart = content.indexOf("《用户协议》")
        val userEnd = userStart + "《用户协议》".length
        val privacyStart = content.indexOf("《隐私政策》")
        val privacyEnd = privacyStart + "《隐私政策》".length

        spannable.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                onClickUserAgreement()
            }
        }, userStart, userEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        spannable.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                onClickPrivacyPolicy()
            }
        }, privacyStart, privacyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        tvContent.text = spannable
        tvContent.movementMethod = LinkMovementMethod.getInstance()
        tvContent.highlightColor = Color.TRANSPARENT

        val dialog = AlertDialog.Builder(context)
            .setView(view)
            .setCancelable(false)
            .create()

        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        btnAgree.setOnClickListener {
            onAgree()
            dialog.dismiss()
        }

        btnExit.setOnClickListener {
            onExit()
            dialog.dismiss()
        }

        dialog.show()
    }
}

MainActivity.kt

XML 复制代码
package com.example.poemapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        PrivacyPolicyDialog(
            context = this,
            onAgree = {
                Toast.makeText(this, "用户已同意", Toast.LENGTH_SHORT).show()
                // TODO: 记录已同意状态
            },
            onExit = {
                finish()
            },
            onClickUserAgreement = {
                // TODO: 跳转用户协议页面
            },
            onClickPrivacyPolicy = {
                // TODO: 跳转隐私政策页面
            }
        ).show()

    }


}
相关推荐
拥小抱7 小时前
Compose 知识点
android
文章永久免费只为良心7 小时前
Web应用安全与防护_第三章_日志文件包含_从一道 CTFshow 题目讲透「日志投毒型 LFI → RCE」:原理、时序陷阱与全变种攻防手册
android·网络安全·渗透测试·漏洞危害·漏洞描述·日志投毒·红队指南
AIsoft_86898 小时前
学术讲座录音如何整理?录音APP可行吗
android·iphone
Patrick在香港10 小时前
一个斜杠,把毫秒变成十几秒
android·数据库·python·性能优化·正则表达式·回溯·redos
海雅达手持终端PDA12 小时前
工业手持终端的“去手机化”演进:看海雅达如何以“同轴扫摄”终结工程妥协,重塑工业PDA新范式
android·物联网·智能手机·硬件工程·能源·制造·iot
booboosui12 小时前
删掉一个住在本机的 agent:8 处要清,顺序不能反
android
hcgeng13 小时前
Android获取崩溃日志
android
aqi0013 小时前
AI编程让鸿蒙开发电子书APP更加快捷
android·华为·harmonyos·鸿蒙·harmony
GY-9313 小时前
【Android】Android Studio 中的协程
android