Anrdroid使用SpannableStringBuilder实现隐私政策和服务协议

背景

本人所在公司的APP被CNAAC(国家移动互联网应用安全管理中心)通报APP没有隐私政策功能而被下架,记录解决该问题的方法。在Android中,TextView 可以通过SpannableStringBuilder设置富文本,并且在文本中可以设置点击事件,以下是实现代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnLogin;
    private CheckBox checkBox;
    private TextView tvPrivacy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();

    }

    private void initView() {
        btnLogin=findViewById(R.id.btn_login);
        checkBox=findViewById(R.id.checkBox);
        tvPrivacy=findViewById(R.id.tv_privacy);
        btnLogin.setOnClickListener(this);
    }
    private void initData() {
//        我同意本DEMO的隐私政策和服务协议
        SpannableStringBuilder builder=new SpannableStringBuilder(getString(R.string.string_privacy));
        ClickableSpan clickSpanPrivacy=new ClickableSpan() {
            @Override
            public void onClick( View widget) {
//                跳转隐私政策网址
                Toast.makeText(MainActivity.this, getString(R.string.string_toast_to_privacy), Toast.LENGTH_SHORT).show();
            }
        };
        builder.setSpan(clickSpanPrivacy,9,13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ClickableSpan clickableSpanProtocol=new ClickableSpan() {
            @Override
            public void onClick( View widget) {
//                跳转服务协议
                Toast.makeText(MainActivity.this, getString(R.string.string_toast_to_service), Toast.LENGTH_SHORT).show();
            }
        };
        builder.setSpan(clickableSpanProtocol,14,18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvPrivacy.setText(builder);
        tvPrivacy.setMovementMethod(LinkMovementMethod.getInstance());//调用此方法时文字点击事件才有效
    }

    @Override
    public void onClick(View v) {
        if(!checkBox.isChecked()){
//            请同意隐私政策和服务协议
            Toast.makeText(this, getString(R.string.string_toast_privacy_service), Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, getString(R.string.string_toast_login_success), Toast.LENGTH_SHORT).show();
        }
    }
}

布局文件:


<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B51A1818"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btn_login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/string_login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:checked="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_login" />

    <TextView
        android:id="@+id/tv_privacy"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/string_privacy"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="@+id/checkBox"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/checkBox"
        app:layout_constraintTop_toTopOf="@+id/checkBox" />
android.support.constraint.ConstraintLayout>

字符串和颜色资源:

<resources>
    <string name="string_login">登录string>
    <string name="string_toast_login_success">登录成功string>
    <string name="string_privacy">我同意本DEMO的隐私政策和服务协议string>
    <string name="string_toast_privacy_service">"请同意隐私政策和服务协议"string>
    <string name="string_toast_to_privacy">跳转隐私政策网址string>
    <string name="string_toast_to_service">跳转服务协议string>
resources>
---------------------------------------------------
<resources>
    <color name="colorPrimary">#008577color>
    <color name="colorPrimaryDark">#00574Bcolor>
    <color name="colorAccent">#D81B60color>
    <color name="white">#ffffffcolor>
resources>

demo下载链接

你可能感兴趣的:(Android)