Android封裝用戶協(xié)議

前言

Android開發(fā)過程中睡榆,我們會涉及到用戶協(xié)議的問題萍肆,因為現(xiàn)在一般的app在沒有設(shè)置用戶協(xié)議的情況下,是不會審核通過的胀屿。用戶協(xié)議不是一個很難的功能塘揣,又必須要有,就像findById()一樣碉纳,沒啥實際用處勿负,但在開發(fā)的時候,又避免不了這樣的代碼劳曹,很是尷尬奴愉。為了節(jié)省開發(fā)時間,這里我將用戶協(xié)議功能做了一個模板封裝類—AgreementDefaultHelper铁孵,方便大家在開發(fā)中使用锭硼。

今天涉及內(nèi)容:

  1. 用戶協(xié)議封裝類使用場景
  2. AgreementDefaultHelper主要方法介紹
  3. AgreementDefaultHelper在Activity中使用
  4. 在用戶協(xié)議界面AgreementActivity的接收處理
  5. 效果圖和項目結(jié)構(gòu)圖
  6. AgreementDefaultHelper源碼

先來波效果圖


1.gif

一.用戶協(xié)議封裝類使用場景

AgreementDefaultHelper做為一個用戶協(xié)議封裝的幫助類,提供了用戶協(xié)議隱私協(xié)議的通用固定文本模板蜕劝。因此它適合在那些對用戶協(xié)議內(nèi)容沒有特殊要求的場景檀头,可以加快app的開發(fā)進程轰异。

二.AgreementDefaultHelper主要方法介紹

AgreementDefaultHelper作為一個用戶協(xié)議封裝的幫助類,具備以下主要方法:

    /**獲取用戶協(xié)議彈框內(nèi)容**/
    public static String getAgreementDialogContent(String appName)

    /***
     * 用戶協(xié)議彈框的設(shè)置(包括彈框內(nèi)容暑始,要點擊的文字的顏色和設(shè)置點擊事件)
     *
     * @param textView 設(shè)置用戶協(xié)議彈框內(nèi)容的textView
     * @param appName app名稱
     * @param textColor 需要點擊字段文字的顏色
     * @param link 聯(lián)系方式搭独。設(shè)置為null的話表示"暫無"
     *             添加電話的示例  Tel: 15927453658
     *             添加qq的示例  QQ: 67030030
     * @param userListener  《用戶協(xié)議》點擊事件
     * @param privacyListener 《隱私協(xié)議》點擊事件
     */
    public static void clickAgreement(Context context,TextView textView, String appName, int textColor, String link, View.OnClickListener userListener, View.OnClickListener privacyListener)

    /**獲取用戶協(xié)議**/
    public static String getUserContent(String appName)

    /**獲取隱私協(xié)議**/
    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();
        //初始化數(shù)據(jù)
        initData();
        //控件監(jiān)聽
        setListener();
    }

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

    private void initData(){

    }

    /**控件監(jiān)聽**/
    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) {
                        //跳轉(zhuǎn)用戶協(xié)議界面的監(jiān)聽
                        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) {
                        //跳轉(zhuǎn)隱私協(xié)議界面的監(jiān)聽
                        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();
        //此處設(shè)置位置窗體大小廊镜,我這里設(shè)置為了手機屏幕寬度的3/4  注意一定要在show方法調(diào)用后再寫設(shè)置窗口大小的代碼牙肝,否則不起效果
        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>

四.在用戶協(xié)議界面AgreementActivity的接收處理

接下來看看用戶協(xié)議界面AgreementActivity的代碼:

/**
 * Title:用戶協(xié)議界面
 * 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"; //跳轉(zhuǎn)tag
    public static final String USER_TYPE = "用戶服務協(xié)議"; //用戶協(xié)議
    public static final String PRIVACY_TYPE = "隱私權(quán)政策"; //隱私協(xié)議

    @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);

        //獲取上一界面數(shù)據(jù)
        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>

五.效果圖和項目結(jié)構(gòu)圖

效果圖.gif
項目結(jié)構(gòu)圖.png

六.AgreementDefaultHelper源碼

下面給出AgreementDefaultHelper類源碼:

還有 53% 的精彩內(nèi)容
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
支付 ¥3.00 繼續(xù)閱讀
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市嗤朴,隨后出現(xiàn)的幾起案子配椭,更是在濱河造成了極大的恐慌,老刑警劉巖雹姊,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件股缸,死亡現(xiàn)場離奇詭異,居然都是意外死亡吱雏,警方通過查閱死者的電腦和手機敦姻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來坎背,“玉大人替劈,你說我怎么就攤上這事〉寐耍” “怎么了陨献?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長懂更。 經(jīng)常有香客問我眨业,道長,這世上最難降的妖魔是什么沮协? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任龄捡,我火速辦了婚禮,結(jié)果婚禮上慷暂,老公的妹妹穿的比我還像新娘聘殖。我一直安慰自己,他們只是感情好行瑞,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布奸腺。 她就那樣靜靜地躺著,像睡著了一般血久。 火紅的嫁衣襯著肌膚如雪突照。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天氧吐,我揣著相機與錄音讹蘑,去河邊找鬼末盔。 笑死,一個胖子當著我的面吹牛座慰,可吹牛的內(nèi)容都是我干的陨舱。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼版仔,長吁一口氣:“原來是場噩夢啊……” “哼隅忿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起邦尊,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎优烧,沒想到半個月后蝉揍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡畦娄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年又沾,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片熙卡。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡杖刷,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出驳癌,到底是詐尸還是另有隱情滑燃,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布颓鲜,位于F島的核電站表窘,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甜滨。R本人自食惡果不足惜乐严,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望衣摩。 院中可真熱鬧昂验,春花似錦、人聲如沸艾扮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽栏渺。三九已至呛梆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間磕诊,已是汗流浹背填物。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工纹腌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓影涉,卻偏偏與公主長得像佣谐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子涎劈,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容