Android 實(shí)現(xiàn)一個隱私彈窗功能
效果圖如下:

1. 設(shè)置同意、退出、點(diǎn)擊用戶協(xié)議、點(diǎn)擊隱私協(xié)議的函數(shù)參數(shù)
2. 《用戶協(xié)議》、《隱私政策》設(shè)置成可點(diǎn)擊的,且顏色要區(qū)分出來
res/layout/dialog_privacy_policy.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="用戶協(xié)議和隱私政策"
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="退出應(yīng)用"
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 文件
<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文件
<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文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#5E5C3F" />
<corners android:radius="8dp" />
</shape>PrivacyPolicyDialog.kt 文件
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 = "在您使用本應(yīng)用之前,請您務(wù)必審慎閱讀、充分理解“用戶協(xié)議”和“隱私政策”各條款內(nèi)容。詳細(xì)資料請閱讀:《用戶協(xié)議》和《隱私政策》。"
val spannable = SpannableString(content)
val userStart = content.indexOf("《用戶協(xié)議》")
val userEnd = userStart + "《用戶協(xié)議》".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
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: 記錄已同意狀態(tài)
},
onExit = {
finish()
},
onClickUserAgreement = {
// TODO: 跳轉(zhuǎn)用戶協(xié)議頁面
},
onClickPrivacyPolicy = {
// TODO: 跳轉(zhuǎn)隱私政策頁面
}
).show()
}
}到此這篇關(guān)于Android 實(shí)現(xiàn)一個隱私彈窗的文章就介紹到這了,更多相關(guān)Android 隱私彈窗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android使用ViewBinding的詳細(xì)步驟(Kotlin簡易版)
最近這段時間在學(xué)習(xí)Kotlin,突然發(fā)現(xiàn)谷歌已經(jīng)把kotlin-android-extensions插件廢棄,目前推薦使用ViewBinding來進(jìn)行替代,接下來通過本文給大家分享Android使用ViewBinding的詳細(xì)步驟,感興趣的朋友一起學(xué)習(xí)吧2021-05-05
Android代碼實(shí)現(xiàn)AdapterViews和RecyclerView無限滾動
這篇文章主要為大家詳細(xì)介紹了Android代碼實(shí)現(xiàn)AdapterViews和RecyclerView無限滾動的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android模擬器實(shí)現(xiàn)手機(jī)添加文件到sd卡的方法
這篇文章主要介紹了Android模擬器實(shí)現(xiàn)手機(jī)添加文件到sd卡的方法,詳細(xì)分析了Android模擬器添加文件到sd卡的步驟與相關(guān)技巧,需要的朋友可以參考下2016-06-06
Android使用Walle實(shí)現(xiàn)多渠道打包功能的實(shí)現(xiàn)示例
這篇文章主要介紹了Android使用Walle實(shí)現(xiàn)多渠道打包功能的實(shí)現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
Android開發(fā)中requestfocus()無效的原因及解決辦法
這篇文章主要介紹了Android開發(fā)中requestfocus()無效的原因及解決辦法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
Android利用WindowManager實(shí)現(xiàn)懸浮窗
這篇文章主要為大家詳細(xì)介紹了Android利用WindowManager實(shí)現(xiàn)懸浮窗效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
什么是Android靜默拍攝 Android靜默拍攝app制作方法
這篇文章主要告訴大家什么是Android靜默拍攝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
android 應(yīng)用內(nèi)部懸浮可拖動按鈕簡單實(shí)現(xiàn)代碼
本篇文章主要介紹了android 應(yīng)用內(nèi)部懸浮可拖動按鈕簡單實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

