当前位置:网站首页>自定义Dialog 实现 仿网易云音乐的隐私条款声明弹框
自定义Dialog 实现 仿网易云音乐的隐私条款声明弹框
2022-07-25 09:21:00 【乐玩兔】

[需求描述]
- 需要自己指定弹框的标题,内容,确定按钮,取消按钮
- 需要给弹框内容的部分文字添加富文本样式和超链接/跳转到指定的Activity
[代码实现]
1. 布局
整体使用垂直的线性布局,将标题,内容,确定,取消等垂直排列,营造一种段落间的层次感
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:gravity="center"
android:layout_gravity="center"
android:background="@drawable/dialog_bg"
>
<TextView
android:id="@+id/privacy_title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#000000"
android:text="服务条款和隐私政策提示"
android:textSize="18sp"/>
<TextView
android:id="@+id/privacy_welcom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/dialog_gray"
android:text="欢迎使用网易云音乐!"
android:lineSpacingMultiplier="1.1"
android:layout_margin="10dp"
android:textSize="16sp" />
<TextView
android:id="@+id/privacy_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/dialog_gray"
android:layout_weight="2"
android:layout_margin="10dp"
android:lineSpacingExtra="1dp"
android:lineSpacingMultiplier="1.1"
android:text="提示内容"
android:textSize="16sp" />
<Button
android:id="@+id/privacy_yes"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:background="@drawable/login_login_shaper"
android:textSize="18sp"
android:textColor="@color/encode_view"
android:text="同 意"/>
<TextView
android:id="@+id/privacy_no"
android:text="不同意并退出APP>>"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
/>
</LinearLayout>
2.自定义Dialog
在自定义的dialog中需要处理布局控件的点击事件,文本显示,还有文本的样式,弹框的位置也需要进行制定.确定/取消 按钮的点击事件的处理通过接口回调来实现
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
/** * Created by Eugenia Gao * Describe:自定义隐私页的dialog * 1.标题和内容 * 2.确定和取消按钮及点击事件 * */
public class PrivacyDialog extends Dialog {
Context context;
String title;
SpannableStringBuilder content;
String confirmBtnText;
SpannableStringBuilder cancelBtnText;
private TextView btnNo;
private TextView tvContent;
private TextView tvTltle;
private TextView tvWelcom;
private Button btnYes;
private ClickInterface clickInterface;
/** * 点击事件的监听接口 */
public interface ClickInterface{
void doCofirm();
void doCancel();
}
/** * 自定义控件需要重写构造函数,传入需要要的值 */
public PrivacyDialog(Context context, String title, SpannableStringBuilder content, String confirmBtnText, SpannableStringBuilder cancelBtnText){
super(context, R.style.MyDialog);
this.context=context;
this.title=title;
this.content=content;
this.confirmBtnText=confirmBtnText;
this.cancelBtnText=cancelBtnText;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
}
/** * 初始化控件 * 1.加载布局 * 2.指定窗口的大小 * 3.文本的设置 */
private void initView() {
View view = LayoutInflater.from(context).inflate(R.layout.privacy_dialog,null);
setContentView(view);
btnNo = view.findViewById(R.id.privacy_no);
tvTltle = view.findViewById(R.id.privacy_title);
tvWelcom = view.findViewById(R.id.privacy_welcom);
tvContent = view.findViewById(R.id.privacy_content);
btnYes = view.findViewById(R.id.privacy_yes);
tvTltle.setText(title);
tvContent.setMovementMethod(LinkMovementMethod.getInstance());
tvContent.setText(content);
btnYes.setText(confirmBtnText);
btnNo.setText(cancelBtnText);
btnNo.setOnClickListener(new ClickListener());
btnYes.setOnClickListener(new ClickListener());
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
dialogWindow.setAttributes(lp);
}
/** * 点击事件的设置-----接口回调 * 设置点击事件,需要重写按钮的确定和取消要执行的操作 */
public void setClickListener(ClickInterface clickInterface){
this.clickInterface=clickInterface;
}
/** * 系统的点击事件的自动监听 */
private class ClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.privacy_no:
if (clickInterface != null) {
clickInterface.doCancel();
}
break;
case R.id.privacy_yes:
if (clickInterface != null) {
clickInterface.doCofirm();
}
break;
}
}
}
}
/** * 用户协议及弹框富文本的点击事件 */
private class textClick extends ClickableSpan {
@Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setUnderlineText(false);
ds.setColor(Color.parseColor("#62839A"));
}
@Override
public void onClick(@NonNull View widget) {
Intent intent = new Intent(WelcomeActivity.this, User_AgreementActivity.class);
intent.putExtra("yinsi", 3);
startActivity(intent);
}
}
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--<!–背景颜色及和透明程度–>-->
<!--<item name="android:windowBackground">@color/result_minor_text</item>-->
<!--<!–是否去除标题 –>-->
<!--<item name="android:windowNoTitle">true</item>-->
<!--<!–是否去除边框–>-->
<!--<item name="android:windowFrame">@null</item>-->
<!--<!–是否浮现在activity之上–>-->
<!--<item name="android:windowIsFloating">true</item>-->
<!--<!–是否模糊–>-->
<!--<item name="android:backgroundDimEnabled">false</item>-->
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
上面版本是适配了AndroidX,高低版本兼容
[在项目中使用]
/** * 隐私条款的dialog弹框 */
@SuppressLint("ResourceAsColor")
private void showPrivacyDialog(final Context context) {
String title ="用户协议和隐私条款提示";
String btnYes="同意";
//弹框内容---------富文本显示
String dialogContent="在使用网易云音乐的服务过程中,我们访问您的各项权限是为了向您提供服务、优化您的服务以及保障您的账号安全等等写你要声明的内容";
int startIndex=dialogContent.indexOf("《");
int contentLength="《服务条款和隐私政策》".length();
SpannableStringBuilder dialogContentStyle = new SpannableStringBuilder(dialogContent);//需要用富文本显示的内容
dialogContentStyle.setSpan(new ForegroundColorSpan(Color.parseColor("#62839A")),startIndex,startIndex+contentLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
dialogContentStyle.setSpan(new textClick(),startIndex,startIndex+contentLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//取消按钮-----------富文本显示
String dialogCancel ="不同意并退出APP>>";
int dialogCancelEnd=dialogCancel.length();
SpannableStringBuilder dialogCancelStyle = new SpannableStringBuilder(dialogCancel);
dialogCancelStyle.setSpan(new ForegroundColorSpan(Color.parseColor("#62839A")),0,dialogCancelEnd, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
final PrivacyDialog privacyDialog = new PrivacyDialog(context, title, dialogContentStyle, btnYes, dialogCancelStyle);
privacyDialog.show();
privacyDialog.setCancelable(false);//点击返回键或者空白处不消失
privacyDialog.setClickListener(new PrivacyDialog.ClickInterface() {
@Override
public void doCofirm() {
privacyDialog.dismiss();
Intent intent = new Intent(WelcomeActivity.this, YinDaoActivity.class);
startActivity(intent);
finish();
}
@Override
public void doCancel() {
privacyDialog.dismiss();
finish();
}
});
}
很简单的一个小功能,具体样式展现可以自己根据自己的需求来定义,大致整体思路就是上面所说的.
本文完~
边栏推荐
- Job 7.15 shell script
- @1-1 CCF 2021-04-1 灰度直方图
- *6-3 节约小能手
- Redis string structure command
- 对象数据如何转化成数组
- Understand the execution process of try, catch and finally (including return) (the most detailed analysis of the whole network)
- 【代码源】每日一题 分数拆分
- Understand why we should rewrite the equals method and hashcode method at the same time + example analysis
- Dynamically add multiple tabs and initialize each tab page
- How to convert object data into arrays
猜你喜欢
![[GKCTF 2021]easynode](/img/f0/1daf6f83fea66fdefd55608cbddac6.png)
[GKCTF 2021]easynode

Week小结

Redis string 结构命令

用kotlin怎么写Android切换界面

cf #785(div2) C. Palindrome Basis

如何将Jar包部署到服务器,注:启动命令有无nohup有很大关系

matplotlib数据可视化三分钟入门,半小时入魔?

Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
![[De1CTF 2019]SSRF Me](/img/12/44c37cc713b49172a10579c9628c94.png)
[De1CTF 2019]SSRF Me

Swagger2 shows that there is a problem with the get interface, which can be solved with annotations
随机推荐
cell的定义
Numpy- array属性、改变形状函数、基本运算
什么是脑裂问题?
Analysis of five data structure principles of redis
【代码源】每日一题 农田划分
链表--基本操作
Excl batch import data, background public parsing method
App的生命周期和AppleDelegate,SceneDelegate
Dynamically add multiple tabs and initialize each tab page
~2 ccf 2022-03-1 未初始化警告
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
js中map()函数的使用
DVWA练习一 暴力破解
@3-2 CCF 2020-12-2 期末预测之最佳阈值
Go foundation 2
How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
~1 ccf 2022-06-2 寻宝!大冒险!
Flex 布局语法与用例
用kotlin怎么写Android切换界面
OC--对象复制