android APP隱私政策彈框的實現代碼實例

android APP隱私政策彈框的實現代碼實例

步驟一:在assets目錄下放置隱私政策的文本文件,比如privacy.txt

步驟二:在drawable目錄下放置圓角彈出框演示:

<code> 

<

shape

xmlns:android

=

"http://schemas.android.com/apk/res/android"

android:shape

=

"rectangle"

>

<

solid

android:color

=

"#ffffff"

/>

<

corners

android:radius

=

"10dp"

/>

shape

>

/<code>

步驟三:在layout下放置彈出框的佈局文件:

<code> 

<

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

=

"wrap_content"

android:layout_height

=

"wrap_content"

android:layout_marginBottom

=

"5dp"

android:text

=

"同意"

android:onClick

=

"onClickAgree"

android:textColor

=

"#000000"

/>

<

Button

android:id

=

"@+id/btn_disagree"

android:layout_width

=

"wrap_content"

android:layout_height

=

"wrap_content"

android:layout_marginBottom

=

"5dp"

android:text

=

"放棄使用"

android:onClick

=

"onClickDisagree"

android:textColor

=

"#000000"

/>

LinearLayout

>

RelativeLayout

>

LinearLayout

>

/<code>

步驟五:放置一個主頁面,測試用:

<code>

<

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"

>

<

CheckBox

android:id

=

"@+id/checkBox"

android:layout_width

=

"wrap_content"

android:layout_height

=

"wrap_content"

/>

<

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

>

/<code>

步驟六:代碼實現如下:

<code>

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

MainActivity

extends

AppCompatActivity

{ Dialog dialog;

protected

void

onCreate

(Bundle savedInstanceState)

{

super

.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

public

void

onClickAgree

(View v)

{ dialog.dismiss(); }

public

void

onClickDisagree

(View v)

{ finish(); }

public

void

onClickPrivacy

(View v)

{ showPrivacy(

"privacy.txt"

); }

public

void

showPrivacy

(String privacyFileName)

{ String str = initAssets(privacyFileName);

final

View inflate = LayoutInflater.from(MainActivity.

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(MainActivity.

this

) .setView(inflate) .show(); 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(); } }

/<code>

步驟7:實現效果如下:


android APP隱私政策彈框的實現代碼實例


分享到:


相關文章: