Android實(shí)現(xiàn)隱私政策彈窗與鏈接功能
1.效果展示
先展示效果,看看是不是你需要的。


2.具體實(shí)現(xiàn)
2.1按鈕美化
在drawable文件夾下新建button_shape.xml
<?xml version="1.0" encoding="utf-8" ?>
<!--相當(dāng)于做了一張圓角的圖片,然后給button作為背景圖片-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--設(shè)置背景色-->
<solid android:color="#F59E27" />
<!--設(shè)置圓角-->
<corners android:radius="105dip" />
<padding
android:bottom="2dp"
android:left="33dp"
android:right="33dp"
android:top="2dp">
</padding>
</shape>
2.2彈窗美化
在drawable文件夾下新建dialog_privacy_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充色 -->
<solid android:color="#ffffff" />
<!-- 矩形圓角半徑 -->
<corners android:radius="10dp" />
</shape>
2.3隱私信息
在assets文件夾下新建privacy.txt,內(nèi)容為彈窗主體信息。
2.4彈窗布局
在layout文件夾下新建一個(gè)布局dialog_privacy_show.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_privacy_shape"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ll_btn_bottom"
android:layout_marginBottom="15dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="羲和隱私政策"
android:textColor="#000000"
android:textSize="18sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:fadingEdgeLength="50dp"
android:requiresFadingEdge="horizontal">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:singleLine="false"
android:text=""
android:textColor="#000000" />
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_btn_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
>
<Button
android:id="@+id/btn_agree"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginRight="15dp"
android:text="同意"
android:onClick="onClickAgree"
android:textColor="#FF0006"
android:background="@drawable/button_shape"/>
<Button
android:id="@+id/btn_disagree"
android:layout_width="130dp"
android:layout_marginBottom="2dp"
android:layout_height="wrap_content"
android:text="放棄使用"
android:onClick="onClickDisagree"
android:textColor="#000000"
android:background="@drawable/button_shape"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
效果:

2.5彈窗鏈接
新建一個(gè)活動(dòng)yinsi.xml
先寫活動(dòng)布局
<LinearLayout android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點(diǎn)擊查看"
android:textSize="14sp"
/>
<TextView
android:id="@+id/tv_xieyi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickPrivacy"
android:text="隱私政策"
android:textColor="#0000ff"
android:textSize="14sp" />
</LinearLayout>
再修改活動(dòng)的java文件,實(shí)現(xiàn)點(diǎn)擊鏈接可以跳出彈窗
package cn.edu.cdut.xihe;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class yinsi extends AppCompatActivity {
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yinsi);
}
public void onClickAgree(View v)
{
dialog.dismiss();
}
public void onClickDisagree(View v)
{
finish();
}
public void onClickPrivacy(View v)
{
showPrivacy("privacy.txt");//放在assets目錄下的隱私政策文本文件
}
public void showPrivacy(String privacyFileName)
{
String str = initAssets(privacyFileName);
final View inflate = LayoutInflater.from(yinsi.this).inflate(R.layout.dialog_privacy_show, null);
TextView tv_title = (TextView) inflate.findViewById(R.id.tv_title);
tv_title.setText("羲和隱私政策");
TextView tv_content = (TextView) inflate.findViewById(R.id.tv_content);
tv_content.setText(str);
dialog = new AlertDialog
.Builder(yinsi.this)
.setView(inflate)
.show();
// 通過(guò)WindowManager獲取
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = dm.widthPixels*4/5;
params.height = dm.heightPixels*1/2;
dialog.getWindow().setAttributes(params);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
}
/**
* 從assets下的txt文件中讀取數(shù)據(jù)
*/
public String initAssets(String fileName) {
String str = null;
try {
InputStream inputStream = getAssets().open(fileName);
str = getString(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
return str;
}
public static String getString(InputStream inputStream) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer("");
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
做到這里,基本完成。
3.進(jìn)一步優(yōu)化
1.由于新建的是一個(gè)活動(dòng),因此該鏈接可以放到其它的布局文件中,用include引入。
2.一般來(lái)說(shuō),用戶首次啟動(dòng)才需要彈窗,可以在主頁(yè)面的啟動(dòng)中加入彈窗程序,并加入一個(gè)判斷是否首次啟動(dòng)。
3.這里點(diǎn)擊鏈接是出現(xiàn)彈窗,更多情況是點(diǎn)擊鏈接會(huì)跳轉(zhuǎn)到相應(yīng)政策頁(yè)面,這里沒(méi)做進(jìn)一步編寫,寫一個(gè)WebView分裝網(wǎng)頁(yè)文件即可。
4.參考資料
本篇內(nèi)容主要參考于博主靈思致遠(yuǎn)Leansmall上傳的資源安卓PrivacyShow隱私彈出框
到此這篇關(guān)于Android:隱私政策彈窗與鏈接的文章就介紹到這了,更多相關(guān)Android隱私政策彈窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
這篇文章主要介紹了Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
安卓(Android) 監(jiān)聽(tīng)按鍵長(zhǎng)按事件
本文主要介紹Android 中監(jiān)聽(tīng)按鍵的長(zhǎng)按事件,對(duì)按鍵機(jī)制進(jìn)行詳解,并附有代碼實(shí)例,具有參考價(jià)值,希望能幫到有需要的小伙伴2016-07-07
Android利用滑動(dòng)菜單框架實(shí)現(xiàn)滑動(dòng)菜單效果
這篇文章主要介紹了Android實(shí)現(xiàn)滑動(dòng)菜單特效之滑動(dòng)菜單框架完全解析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法,涉及Android日期時(shí)間相關(guān)轉(zhuǎn)換與運(yùn)算操作技巧,需要的朋友可以參考下2018-01-01
Flutter本地存儲(chǔ)之基本的鍵值對(duì)存儲(chǔ)詳解
在原生的?Android?或?iOS?中,都提供了基本的鍵值對(duì)存儲(chǔ)方式,在?Flutter?中,提供了?shared_preferences?這個(gè)插件來(lái)實(shí)現(xiàn)本地鍵值對(duì)數(shù)據(jù)存儲(chǔ),本文就來(lái)和大家簡(jiǎn)單聊聊吧2023-03-03

