前言
在Android
開發(fā)過程中睡榆,我們會涉及到用戶協(xié)議
的問題萍肆,因為現(xiàn)在一般的app
在沒有設(shè)置用戶協(xié)議
的情況下,是不會審核通過的胀屿。用戶協(xié)議
不是一個很難的功能塘揣,又必須要有,就像findById()
一樣碉纳,沒啥實際用處勿负,但在開發(fā)的時候,又避免不了這樣的代碼劳曹,很是尷尬奴愉。為了節(jié)省開發(fā)時間,這里我將用戶協(xié)議
功能做了一個模板封裝類—AgreementDefaultHelper
铁孵,方便大家在開發(fā)中使用锭硼。
今天涉及內(nèi)容:
- 用戶協(xié)議封裝類使用場景
- AgreementDefaultHelper主要方法介紹
- AgreementDefaultHelper在Activity中使用
- 在用戶協(xié)議界面AgreementActivity的接收處理
- 效果圖和項目結(jié)構(gòu)圖
- 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中使用
下面給出AgreementDefaultHelper
在Activity
中使用代碼:
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
類源碼: