Android封装用户协议

前言

Android开发过程中,我们会涉及到用户协议的问题,因为现在一般的app在没有设置用户协议的情况下,是不会审核通过的。用户协议不是一个很难的功能,又必须要有,就像findById()一样,没啥实际用处,但在开发的时候,又避免不了这样的代码,很是尴尬。为了节省开发时间,这里我将用户协议功能做了一个模板封装类—AgreementDefaultHelper,方便大家在开发中使用。

今天涉及内容:

  1. 用户协议封装类使用场景
  2. AgreementDefaultHelper主要方法介绍
  3. AgreementDefaultHelper在Activity中使用
  4. 在用户协议界面AgreementActivity的接收处理
  5. 效果图和项目结构图
  6. AgreementDefaultHelper源码

先来波效果图


1.gif

一.用户协议封装类使用场景

AgreementDefaultHelper做为一个用户协议封装的帮助类,提供了用户协议隐私协议的通用固定文本模板。因此它适合在那些对用户协议内容没有特殊要求的场景,可以加快app的开发进程。

二.AgreementDefaultHelper主要方法介绍

AgreementDefaultHelper作为一个用户协议封装的帮助类,具备以下主要方法:

    /**获取用户协议弹框内容**/
    public static String getAgreementDialogContent(String appName)

    /***
     * 用户协议弹框的设置(包括弹框内容,要点击的文字的颜色和设置点击事件)
     *
     * @param textView 设置用户协议弹框内容的textView
     * @param appName app名称
     * @param textColor 需要点击字段文字的颜色
     * @param link 联系方式。设置为null的话表示"暂无"
     *             添加电话的示例  Tel: 15927453658
     *             添加qq的示例  QQ: 67030030
     * @param userListener  《用户协议》点击事件
     * @param privacyListener 《隐私协议》点击事件
     */
    public static void clickAgreement(Context context,TextView textView, String appName, int textColor, String link, View.OnClickListener userListener, View.OnClickListener privacyListener)

    /**获取用户协议**/
    public static String getUserContent(String appName)

    /**获取隐私协议**/
    public static String getPrivacyContent(String appName)

三.AgreementDefaultHelper在Activity中使用

下面给出AgreementDefaultHelperActivity中使用代码:

public class TempActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView mTvTest;
    private Button mBtnTest;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temp);

        //初始化控件
        initView();
        //初始化数据
        initData();
        //控件监听
        setListener();
    }

    /**初始化控件**/
    private void initView(){
        mTvTest=findViewById(R.id.mTvTest);
        mBtnTest=findViewById(R.id.mBtnTest);
    }

    private void initData(){

    }

    /**控件监听**/
    private void setListener() {
        mBtnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    /**初始化并弹出对话框方法**/
    private void showDialog(){
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout,null,false);
        AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();

        TextView textView = view.findViewById(R.id.tv);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);

        AgreementDefaultHelper.clickAgreement(this, textView, "测试app",
                R.color.blue, null,
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转用户协议界面的监听
                        Intent intent=new Intent(TempActivity.this,AgreementActivity.class);
                        intent.putExtra( AgreementActivity.AGREEMENT_TAG,AgreementActivity.USER_TYPE);
                        startActivity(intent);
                    }
                },
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转隐私协议界面的监听
                        Intent intent=new Intent(TempActivity.this,AgreementActivity.class);
                        intent.putExtra(AgreementActivity.AGREEMENT_TAG,AgreementActivity.PRIVACY_TYPE);
                        startActivity(intent);
                    }
                });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                //取消,退出app
                ToastUtil.shortShow("====退出app======");
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                //存储同意标记
                //......

                //同意,进入app流程
                ToastUtil.shortShow("====同意,进入app流程======");
            }
        });
        dialog.show();
        //此处设置位置窗体大小,我这里设置为了手机屏幕宽度的3/4  注意一定要在show方法调用后再写设置窗口大小的代码,否则不起效果
        dialog.getWindow().setLayout(ScreenUtil.getWidth()*3/4, LinearLayout.LayoutParams.WRAP_CONTENT);
    }

}

其中AlertDialog的布局dialog_layout.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:pain="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:paddingBottom="5dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="16sp"
        android:textColor="@color/black"/>

    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="2dp"
        android:layout_marginEnd="5dp"
        app:layout_constraintEnd_toStartOf="@+id/btn_confirm"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv"
        android:background="@color/color_e3e3e3"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:text="取消"/>

    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="2dp"
        android:background="@color/green"
        app:layout_constraintBottom_toBottomOf="@+id/btn_cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_cancel"
        app:layout_constraintTop_toTopOf="@+id/btn_cancel"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:text="确定"/>

</androidx.constraintlayout.widget.ConstraintLayout>

四.在用户协议界面AgreementActivity的接收处理

接下来看看用户协议界面AgreementActivity的代码:

/**
 * Title:用户协议界面
 * description:
 * autor:pei
 * created on 2020/11/17
 */
public class AgreementActivity extends AppCompatActivity {

    private TextView mTvTitle;
    private TextView mTvContent;

    public static final String AGREEMENT_TAG = "agreement_tag"; //跳转tag
    public static final String USER_TYPE = "用户服务协议"; //用户协议
    public static final String PRIVACY_TYPE = "隐私权政策"; //隐私协议

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agreement);

        mTvTitle=findViewById(R.id.tv_title);
        mTvContent=findViewById(R.id.tv_content);

        //获取上一界面数据
        String tag= getIntent().getStringExtra(AGREEMENT_TAG);
        String content=null;
        switch (tag) {
            case USER_TYPE:
                content= AgreementDefaultHelper.getUserContent("测试app");
                break;
            case PRIVACY_TYPE:
                content=AgreementDefaultHelper.getPrivacyContent("测试app");
                break;
            default:
                break;
        }
        mTvTitle.setText(tag);
        mTvContent.setText(content);
    }

}

AgreementActivity对应布局activity_agreement.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:pain="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="40dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="@color/blue"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:gravity="center"/>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nsv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title">

        <TextView
            android:id="@+id/tv_content"
            style="@style/tv_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/black"
            android:textSize="16sp" />
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

五.效果图和项目结构图

效果图.gif
项目结构图.png

六.AgreementDefaultHelper源码

下面给出AgreementDefaultHelper类源码:

还有 53% 的精彩内容
©著作权归作者所有,转载或内容合作请联系作者
支付 ¥3.00 继续阅读
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容