一、概述
根据法规,现在上架的应用都要在App启动的时候弹出用户协议和隐私政策相关的内容,以便告知用户在使用App过程中的一些注意事项之类的东西,如果App没有添加这个功能,则不允许上架。
二、实现
final PrivacyDialog dialog = new PrivacyDialog(MainActivity.this);
TextView tv_privacy_tips = dialog.findViewById(R.id.tv_privacy_tips);
TextView btn_exit = dialog.findViewById(R.id.btn_exit);
TextView btn_enter = dialog.findViewById(R.id.btn_enter);
dialog.show();
String string = getResources().getString(R.string.privacy_tips);
String key1 = getResources().getString(R.string.privacy_tips_key1);
String key2 = getResources().getString(R.string.privacy_tips_key2);
int index1 = string.indexOf(key1);
int index2 = string.indexOf(key2);
//需要显示的字串
SpannableString spannedString = new SpannableString(string);
//设置点击字体颜色
ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(getResources().getColor(R.color.colorBlue));
spannedString.setSpan(colorSpan1, index1, index1 + key1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(getResources().getColor(R.color.colorBlue));
spannedString.setSpan(colorSpan2, index2, index2 + key2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置点击字体大小
AbsoluteSizeSpan sizeSpan1 = new AbsoluteSizeSpan(18, true);
spannedString.setSpan(sizeSpan1, index1, index1 + key1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
AbsoluteSizeSpan sizeSpan2 = new AbsoluteSizeSpan(18, true);
spannedString.setSpan(sizeSpan2, index2, index2 + key2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置点击事件
ClickableSpan clickableSpan1 = new ClickableSpan() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, TermsActivity.class);
startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
//点击事件去掉下划线
ds.setUnderlineText(false);
}
};
spannedString.setSpan(clickableSpan1, index1, index1 + key1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
ClickableSpan clickableSpan2 = new ClickableSpan() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, PrivacyPolicyActivity.class);
startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
//点击事件去掉下划线
ds.setUnderlineText(false);
}
};
spannedString.setSpan(clickableSpan2, index2, index2 + key2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置点击后的颜色为透明,否则会一直出现高亮
tv_privacy_tips.setHighlightColor(Color.TRANSPARENT);
//开始响应点击事件
tv_privacy_tips.setMovementMethod(LinkMovementMethod.getInstance());
tv_privacy_tips.setText(spannedString);
//设置弹框宽度占屏幕的80%
WindowManager m = getWindowManager();
Display defaultDisplay = m.getDefaultDisplay();
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = (int) (defaultDisplay.getWidth() * 0.80);
dialog.getWindow().setAttributes(params);
btn_exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
SPUtil.put(MainActivity.this, SP_VERSION_CODE, currentVersionCode);
SPUtil.put(MainActivity.this, SP_PRIVACY, false);
finish();
}
});
btn_enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
SPUtil.put(MainActivity.this, SP_VERSION_CODE, currentVersionCode);
SPUtil.put(MainActivity.this, SP_PRIVACY, true);
Toast.makeText(MainActivity.this, getString(R.string.confirmed), Toast.LENGTH_SHORT).show();
}
});
首先利用SpannableString将需要显示的字串加载进来,然后分别通过ForegroundColorSpan和AbsoluteSizeSpan设置需要高亮显示字串的颜色和大小,这里的高亮字串就是我们要点击的“用户协议”和“隐私政策”,最后通过调用TextView的setMovementMethod方法为高亮字串设置点击事件。
这里有两点需要注意:
1、点击高亮字串后颜色会一直显示高亮,此时需要设置颜色为透明setHighlightColor(Color.TRANSPARENT);
2、部分手机会出现高亮字串下面有一条横线,此时需要设置它不显示setUnderlineText(false);
到此Android的用户协议和隐私政策实现方式就讲完了,有需要下载源码的小伙伴点击下面链接下载: