当前位置:网站首页>Customize dialog to realize the pop-up box of privacy clause statement imitating Netease cloud music
Customize dialog to realize the pop-up box of privacy clause statement imitating Netease cloud music
2022-07-25 09:44:00 【Happy play rabbit】

[ Requirements describe ]
- You need to specify the title of the pop-up box by yourself , Content , OK button , Cancel button
- You need to add rich text styles and hyperlinks to some of the text in the pop-up box / Jump to the specified Activity
[ Code implementation ]
1. Layout
Use vertical linear layout as a whole , Add the title , Content , determine , Cancel equal vertical arrangement , Create a sense of hierarchy between paragraphs
<?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=" Service terms and privacy policy tips "
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=" Welcome to Netease cloud music !"
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=" Prompt content "
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=" Same as It means "/>
<TextView
android:id="@+id/privacy_no"
android:text=" Disagree and quit APP>>"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
/>
</LinearLayout>
2. Customize Dialog
In the custom dialog You need to handle the click event of the layout control , Text display , And the style of text , The position of the spring frame also needs to be determined . determine / Cancel The button click event is handled through the interface callback
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: Customize the privacy page dialog * 1. Title and content * 2. Confirm and Cancel buttons and click events * */
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;
/** * Click the event listening interface */
public interface ClickInterface{
void doCofirm();
void doCancel();
}
/** * Custom controls need to override constructors , Pass in the required value */
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();
}
/** * Initialize control * 1. Load layout * 2. Specifies the size of the window * 3. Text settings */
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(); // Get screen width 、 High use
lp.width = (int) (d.widthPixels * 0.8); // The height is set to the height of the screen 0.6
dialogWindow.setAttributes(lp);
}
/** * Click event settings ----- Interface callback * Set click event , You need to rewrite the button to confirm and cancel the operation to be performed */
public void setClickListener(ClickInterface clickInterface){
this.clickInterface=clickInterface;
}
/** * Automatic monitoring of system click events */
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;
}
}
}
}
/** * User agreement and pop-up rich text click events */
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">
<!--<!– Background color and transparency –>-->
<!--<item name="android:windowBackground">@color/result_minor_text</item>-->
<!--<!– Whether to remove the title –>-->
<!--<item name="android:windowNoTitle">true</item>-->
<!--<!– Whether to remove the border –>-->
<!--<item name="android:windowFrame">@null</item>-->
<!--<!– Whether it appears in activity above –>-->
<!--<item name="android:windowIsFloating">true</item>-->
<!--<!– Whether it's fuzzy –>-->
<!--<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>
The above version is adapted AndroidX, High and low versions are compatible
[ Used in projects ]
/** * Privacy clause dialog bounced */
@SuppressLint("ResourceAsColor")
private void showPrivacyDialog(final Context context) {
String title =" Tips on user agreement and privacy terms ";
String btnYes=" agree! ";
// Pop up the contents --------- Rich text display
String dialogContent=" In the process of using Netease cloud music service , We access your permissions to provide you with services 、 Optimize your service and ensure the security of your account, etc. write the content you want to declare ";
int startIndex=dialogContent.indexOf("《");
int contentLength="《 Terms of service and privacy policy 》".length();
SpannableStringBuilder dialogContentStyle = new SpannableStringBuilder(dialogContent);// Content that needs to be displayed in rich text
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);
// Cancel button ----------- Rich text display
String dialogCancel =" Disagree and quit 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);// Click the back button or the blank space does not disappear
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();
}
});
}
A very simple little function , The specific style presentation can be defined according to your own needs , The general idea is the above .
The end of this paper ~
边栏推荐
- 初识Opencv4.X----为图像添加高斯噪声
- 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
- matlab的find()函数的一些用法(快速查找符合条件的值)
- laravel 调用第三方 发送邮件 (php)
- 初识Opencv4.X----均值滤波
- 初识Opencv4.X----图像直方图绘制
- How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
- Singleton mode
- 初识Opencv4.X----为图像添加椒盐噪声
- Android 如何使用adb命令查看应用本地数据库
猜你喜欢
随机推荐
Job 7.15 shell script
Flex 布局语法与用例
微信小程序初步了解及实现底部导航栏
基于树莓派4b的传感器数据可视化实现
Voice chat app source code - produced by NASS network source code
初识Opencv4.X----图像卷积
OC -- Foundation -- Collection
Object initialization
Constant power wireless charging based on stm32
卷积神经网络发展历程(部分)
OC--初识
Indexes, views and transactions of MySQL
[gplt] 2022 popular lover (Floyd)
Singleton mode
cf #785(div2) C. Palindrome Basis
换电脑后如何配置SSH
Flutter Rive 多状态例子
How to convert object data into arrays
OC--Foundation--字典
Why use json.stringify() and json.parse()








