iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现隐私政策弹窗与链接功能
  • 556
分享到

Android实现隐私政策弹窗与链接功能

2024-04-02 19:04:59 556人浏览 泡泡鱼
摘要

1.效果展示 先展示效果,看看是不是你需要的。 2.具体实现  2.1按钮美化 在drawable文件夹下新建button_shape.xml <?x

1.效果展示

先展示效果,看看是不是你需要的。

在这里插入图片描述
在这里插入图片描述

2.具体实现

 2.1按钮美化

在drawable文件夹下新建button_shape.xml


<?xml version="1.0" encoding="utf-8" ?>
<!--相当于做了一张圆角的图片,然后给button作为背景图片-->
<shape xmlns:Android="Http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--设置背景色-->
    <solid android:color="#F59E27" />
    <!--设置圆角-->
    <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,内容为弹窗主体信息。

2.4弹窗布局

在layout文件夹下新建一个布局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弹窗链接

新建一个活动yinsi.xml
先写活动布局


<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="点击查看"
        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>

再修改活动的java文件,实现点击链接可以跳出弹窗


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();
        // 通过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);
    }
    
    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.进一步优化

1.由于新建的是一个活动,因此该链接可以放到其它的布局文件中,用include引入。
2.一般来说,用户首次启动才需要弹窗,可以在主页面的启动中加入弹窗程序,并加入一个判断是否首次启动。
3.这里点击链接是出现弹窗,更多情况是点击链接会跳转到相应政策页面,这里没做进一步编写,写一个WEBView分装网页文件即可。

4.参考资料

本篇内容主要参考于博主灵思致远Leansmall上传的资源安卓PrivacyShow隐私弹出框

到此这篇关于Android:隐私政策弹窗与链接的文章就介绍到这了,更多相关Android隐私政策弹窗内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Android实现隐私政策弹窗与链接功能

本文链接: https://www.lsjlt.com/news/130330.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android实现隐私政策弹窗与链接功能
    1.效果展示 先展示效果,看看是不是你需要的。 2.具体实现  2.1按钮美化 在drawable文件夹下新建button_shape.xml <?x...
    99+
    2022-11-12
  • Android 实现隐私政策提示弹窗(完整版)
    android studio版本:2021.2.1 例程名称:pravicydialog 功能:1、启动app后弹窗隐私协议2、屏蔽返回键3、再次启动不再显示隐私协议。 本例程的绝大部分代码来自下面链接,因为本人改了一些,增加了一些功...
    99+
    2023-09-02
    android android studio 隐私协议弹窗
  • Android隐私弹框功能的实现
    编写Android的APP进去点击的用户隐私弹框 因为现在发布APP现在都需要在发布前生成一个用户隐私弹框让用户点击,所以手写了一个用以使用 效果图 java代码 public...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作