app第一次安装需要弹出同意服务协议和隐私政策,效果如下:
此处使用AlertDialog作为展示
布局
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical"
android:background="@drawable/corner_shape_white"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btm1"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="服务协议和隐私政策"
android:textColor="@color/black"
android:textSize="18sp"/>
<TextView
android:id="@+id/userArgTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/user_arg_content"
android:textColor="@color/black"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<Button
android:id="@+id/notAgreeBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="暂不同意"
android:textSize="17sp"
android:layout_marginRight="5dp"
android:textColor="@color/black"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/corner_bg_no_theme_selector"/>
<Button
android:id="@+id/agreeBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="同意并接受"
android:textSize="17sp"
android:layout_marginLeft="5dp"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/corner_bg_theme_selector"
android:textColor="@color/black" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
public class ShowAgrementDialog extends AlertDialog {
private Context context;
private TextView agreeTv;
public static final int ARGEE_TEXT_CLICK = 1; //用户协议
public static final int SECRET_TEXT_CLICK = 2; //隐私协议
public static final int ARGEE_BTN_CLICK = 3; //同意按钮
public static final int NOT_ARGEE_BTN_CLICK = 4; //不同意按钮
private static int START_AG = 0; //用户协议需要加颜色的开始文字位置
private static int END_AG = 0;//结束
private static int START_SECRET = 0;//隐私开始文字位置
private static int END_SECRET = 0;//结束
private OnBtnClickListener listener;
public ShowAgrementDialog(Context context) {
super(context);
this.context = context;
}
public interface OnBtnClickListener {
void onClick(int type);
}
public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
this.listener = onBtnClickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_show_user_agreement);
initView();
}
private void initView() {
agreeTv = findViewById(R.id.userArgTv);
String argContent = context.getResources().getString(R.string.user_arg_content);
START_AG = argContent.indexOf("《服务协议》");
END_AG = START_AG + "《服务协议》".length();
START_SECRET = argContent.indexOf("《隐私政策》");
END_SECRET = START_SECRET + "《隐私政策》".length();
Log.d("测试起", START_AG + "");
Log.d("测试后", END_AG + "");
SpannableString spannableString = new SpannableString(argContent);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.btntextcolor_blue));
ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(context.getResources().getColor(R.color.btntextcolor_blue));
UnderlineSpan underlineSpan = new UnderlineSpan();
UnderlineSpan underlineSpan2 = new UnderlineSpan();
ClickableSpan userArgeeClick = new ClickableSpan() {//用户协议
@Override
public void onClick(@NonNull View view) {
Toast.makeText(context, "用户协议", Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setUnderlineText(false);//去除连接下划线
ds.setColor(context.getResources().getColor(R.color.btntextcolor_blue));
ds.clearShadowLayer();
}
};
ClickableSpan secretClick = new ClickableSpan() {//隐私政策
@Override
public void onClick(@NonNull View view) {
Toast.makeText(context, "隐私政策", Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
ds.setColor(context.getResources().getColor(R.color.btntextcolor_blue));
ds.setUnderlineText(false);//去除连接下划线
ds.clearShadowLayer();
}
};
findViewById(R.id.agreeBtn).setOnClickListener(view -> listener.onClick(ARGEE_BTN_CLICK));
findViewById(R.id.notAgreeBtn).setOnClickListener(view -> listener.onClick(NOT_ARGEE_BTN_CLICK));
spannableString.setSpan(colorSpan, START_AG, END_AG, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableString.setSpan(underlineSpan, START_AG + 1, END_AG - 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableString.setSpan(userArgeeClick, START_AG, END_AG, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableString.setSpan(colorSpan2, START_SECRET, END_SECRET, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableString.setSpan(underlineSpan2, START_SECRET + 1, END_SECRET - 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
spannableString.setSpan(secretClick, START_SECRET, END_SECRET, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
agreeTv.setMovementMethod(LinkMovementMethod.getInstance());
//设置点击背景色透明 解决点击时有阴影效果
agreeTv.setHighlightColor(context.getResources().getColor(android.R.color.transparent));
agreeTv.setText(spannableString);
setCanceledOnTouchOutside(false);
setCancelable(false);
}
}
最后在引导页调用
ShowAgrementDialog showAgrementDialog = new ShowAgrementDialog(this);
showAgrementDialog.show();
showAgrementDialog.setOnBtnClickListener(type -> {
switch (type) {
case ShowAgrementDialog.NOT_ARGEE_BTN_CLICK://不同意按钮
showAgrementDialog.dismiss();
finish();
break;
case ShowAgrementDialog.ARGEE_BTN_CLICK: //同意按钮
showAgrementDialog.dismiss();
requestPermission();
break;
}
});