此文主要解决部分接入者不知道如何设置点击事件以及设置的View没有和预想的一样全屏问题。setAuthContentView 方法参数为 View, 开发者可以对 view 及其子 View 进行更多的操作,因此 setAuthContentView 相较于 setAuthLayoutResID 更加灵活。
目录
1. 准备工作
1.1 已经接入好 SDK,使用默认配置能够运行。
1.2 根据公司产品要求调用SDK 提供的 API 调好SDK "本机号码一键登录" 按钮、号码栏、协议栏的位置。
2. setAuthContentView的调用
2.1 准备好要添加到SDK授权页的XML文件,所有的View位置都通过 margin 值来确定,这样就能与 SDK 三要素同步以此保证所有控件的相对位置基本准确。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:text="出门右转新东方烹饪学校"
android:textColor="@color/red"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="100dp"
android:text="欢迎登录蓝翔挖掘机学校"
android:textColor="@color/red"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="170dp"
android:text="本机号码"
android:textColor="@color/red"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="190dp"
android:layout_marginEnd="16dp"
android:text="点击进入富婆通讯录"
android:textColor="@color/red" />
<Button
android:id="@+id/btn_test_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="中国移动"
android:textColor="@color/red"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_test_error"
android:layout_marginTop="45dp"
android:layout_toStartOf="@+id/tv_z"
android:background="@color/blue"
android:padding="8dp"
android:text="神"
android:textColor="@color/white"
android:textSize="24sp" />
<TextView
android:id="@+id/tv_z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_test_error"
android:layout_marginStart="16dp"
android:layout_marginTop="45dp"
android:layout_toStartOf="@+id/tv_x"
android:background="@color/blue"
android:padding="8dp"
android:text="州"
android:textColor="@color/white"
android:textSize="24sp" />
<TextView
android:id="@+id/tv_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_test_error"
android:layout_marginStart="16dp"
android:layout_marginTop="45dp"
android:layout_toStartOf="@+id/hold_view"
android:background="@color/blue"
android:padding="8dp"
android:text="行"
android:textColor="@color/white"
android:textSize="24sp" />
<View
android:id="@+id/hold_view"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_below="@+id/btn_test_error"
android:layout_centerHorizontal="true"
android:layout_marginStart="8dp"
android:layout_marginTop="45dp"
android:layout_marginEnd="8dp" />
<TextView
android:id="@+id/tv_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_test_error"
android:layout_marginTop="45dp"
android:layout_toEndOf="@+id/hold_view"
android:background="@color/blue"
android:padding="8dp"
android:text="我"
android:textColor="@color/white"
android:textSize="24sp" />
<TextView
android:id="@+id/tv_k"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_test_error"
android:layout_marginStart="16dp"
android:layout_marginTop="45dp"
android:layout_toEndOf="@+id/tv_w"
android:background="@color/blue"
android:padding="8dp"
android:text="看"
android:textColor="@color/white"
android:textSize="24sp" />
<TextView
android:id="@+id/tv_xi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_test_error"
android:layout_marginStart="16dp"
android:layout_marginTop="45dp"
android:layout_toEndOf="@+id/tv_k"
android:background="@color/blue"
android:padding="8dp"
android:text="行"
android:textColor="@color/white"
android:textSize="24sp" />
</RelativeLayout>
AS 预览如下图所示:
2.2 通过 LayoutInflater 生成 setAuthContentView方法所需的View,并设置指定View的点击事件。
val contentView: View = LayoutInflater.from(this@MainActivity)
.inflate(R.layout.test_error, null)
.apply {
//通过findViewById获取到要设置点击事件的View并设置点击事件。
this.findViewById<Button>(R.id.btn_test_error).setOnClickListener {
//开发者自己的业务,这里弹出一个Toast
Toast.makeText(this@MainActivity,"点击了“中国移动”按钮", Toast.LENGTH_SHORT).show()
}
//为什么要设置LayoutParams,请阅读LayoutInflater源码。
this.layoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT)
}
mHelper?.authThemeConfig = AuthThemeConfig.Builder().apply {
setAuthContentView(contentView)
setLogBtn(-1, 45)
setLogBtnMargin(30, 30)
setLogBtnClickListener(object : LoginClickListener {
override fun onLoginClickStart(p0: Context?, p1: JSONObject?) {
MyLog.e("======开始点击=====")
}
override fun onLoginClickComplete(p0: Context?, p1: JSONObject?) {
MyLog.e("=====点击结束=====")
}
})
setStatusBar(0xFFFFFFFF.toInt(), true)
setCheckTipText("")
setNumberOffsetX(50)
setPrivacyAlignment(
"手机号登录即表示您同意免责申明及" + AuthThemeConfig.PLACEHOLDER2,
"免责申明",
"https://www.cmpassport.com",null,null
)
setPrivacyText(
14,
ContextCompat.getColor(this@MainActivity, R.color.blue),
ContextCompat.getColor(this@MainActivity, R.color.red),
true
)
setPrivacyMargin(0, 0)
}.build()
3. 运行效果图
PS:关于LayoutInflate的inflate(R.layout.你的xml, null)创建出来的View为什么会不能全屏的问题如果觉得观看源码难度较大可以进行debug分析: