您好,登錄后才能下訂單哦!
這篇文章主要講解了“Android怎么實(shí)現(xiàn)隱私政策彈窗與鏈接功能”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Android怎么實(shí)現(xiàn)隱私政策彈窗與鏈接功能”吧!
先展示效果,看看是不是你需要的。
在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>
在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>
在assets文件夾下新建privacy.txt,內(nèi)容為彈窗主體信息。
在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>
效果:
新建一個(gè)活動(dòng)yinsi.xml
先寫(xiě)活動(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(" "); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } }
1.由于新建的是一個(gè)活動(dòng),因此該鏈接可以放到其它的布局文件中,用include引入。
2.一般來(lái)說(shuō),用戶(hù)首次啟動(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)一步編寫(xiě),寫(xiě)一個(gè)WebView分裝網(wǎng)頁(yè)文件即可。
感謝各位的閱讀,以上就是“Android怎么實(shí)現(xiàn)隱私政策彈窗與鏈接功能”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Android怎么實(shí)現(xiàn)隱私政策彈窗與鏈接功能這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。