feat(ui): CommonDialog 支持垂直3按钮布局(iOS风格)
This commit is contained in:
@@ -26,8 +26,10 @@ open class CommonDialog(
|
||||
private var contentText: String? = null
|
||||
private var negativeText = "取消"
|
||||
private var positiveText = "确认"
|
||||
private var neutralText: String? = null
|
||||
private var negativeClick: (() -> Unit)? = null
|
||||
private var positiveClick: (() -> Unit)? = null
|
||||
private var neutralClick: (() -> Unit)? = null
|
||||
private var dismissCallback: (() -> Unit)? = null
|
||||
|
||||
/** 设置标题,为空时隐藏 */
|
||||
@@ -36,18 +38,24 @@ open class CommonDialog(
|
||||
/** 设置内容,为空时隐藏 */
|
||||
fun setContent(text: String): CommonDialog = apply { contentText = text }
|
||||
|
||||
/** 设置左侧取消按钮文字及点击回调 */
|
||||
/** 设置左侧取消按钮文字及点击回调(3按钮模式下对应顶部按钮) */
|
||||
fun setNegativeButton(text: String, onClick: (() -> Unit)? = null): CommonDialog = apply {
|
||||
negativeText = text
|
||||
negativeClick = onClick
|
||||
}
|
||||
|
||||
/** 设置右侧确认按钮文字及点击回调 */
|
||||
/** 设置右侧确认按钮文字及点击回调(3按钮模式下对应底部按钮) */
|
||||
fun setPositiveButton(text: String, onClick: (() -> Unit)? = null): CommonDialog = apply {
|
||||
positiveText = text
|
||||
positiveClick = onClick
|
||||
}
|
||||
|
||||
/** 设置中间第三个按钮文字及点击回调,调用后自动切换为垂直3按钮布局 */
|
||||
fun setNeutralButton(text: String, onClick: (() -> Unit)? = null): CommonDialog = apply {
|
||||
neutralText = text
|
||||
neutralClick = onClick
|
||||
}
|
||||
|
||||
/** 设置弹窗消失回调 */
|
||||
fun setOnDismissCallback(callback: () -> Unit): CommonDialog = apply {
|
||||
dismissCallback = callback
|
||||
@@ -70,6 +78,8 @@ open class CommonDialog(
|
||||
val verticalMargin = if (GlobalData.deviceRole == DeviceRole.MASTER) 35.dp else 20.dp
|
||||
binding.tvTitle.updateLayoutParams<LinearLayout.LayoutParams> { topMargin = verticalMargin }
|
||||
binding.tvContent.updateLayoutParams<LinearLayout.LayoutParams> { bottomMargin = verticalMargin }
|
||||
|
||||
// 2按钮点击事件
|
||||
binding.btnLeft.setOnClickListener {
|
||||
negativeClick?.invoke()
|
||||
dismiss()
|
||||
@@ -78,6 +88,21 @@ open class CommonDialog(
|
||||
positiveClick?.invoke()
|
||||
dismiss()
|
||||
}
|
||||
|
||||
// 3按钮点击事件(negativeClick/positiveClick 与2按钮模式共用)
|
||||
binding.btnTop.setOnClickListener {
|
||||
negativeClick?.invoke()
|
||||
dismiss()
|
||||
}
|
||||
binding.btnMiddle.setOnClickListener {
|
||||
neutralClick?.invoke()
|
||||
dismiss()
|
||||
}
|
||||
binding.btnBottom.setOnClickListener {
|
||||
positiveClick?.invoke()
|
||||
dismiss()
|
||||
}
|
||||
|
||||
setOnDismissListener { dismissCallback?.invoke() }
|
||||
}
|
||||
|
||||
@@ -87,8 +112,21 @@ open class CommonDialog(
|
||||
binding.tvContent.text = contentText
|
||||
binding.tvTitle.visibility = if (titleText.isNullOrEmpty()) View.GONE else View.VISIBLE
|
||||
binding.tvContent.visibility = if (contentText.isNullOrEmpty()) View.GONE else View.VISIBLE
|
||||
binding.btnLeft.text = negativeText
|
||||
binding.btnRight.text = positiveText
|
||||
|
||||
val hasThreeButtons = !neutralText.isNullOrEmpty()
|
||||
binding.layoutButtonsHorizontal.visibility = if (hasThreeButtons) View.GONE else View.VISIBLE
|
||||
binding.layoutButtonsVertical.visibility = if (hasThreeButtons) View.VISIBLE else View.GONE
|
||||
|
||||
if (hasThreeButtons) {
|
||||
// 3按钮模式:顶部=取消,中间=中立,底部=确认
|
||||
binding.btnTop.text = negativeText
|
||||
binding.btnMiddle.text = neutralText
|
||||
binding.btnBottom.text = positiveText
|
||||
} else {
|
||||
// 2按钮模式
|
||||
binding.btnLeft.text = negativeText
|
||||
binding.btnRight.text = positiveText
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,9 @@
|
||||
android:layout_height="1dp"
|
||||
android:background="@color/gray_eb" />
|
||||
|
||||
<!-- 2个按钮时显示(水平排列) -->
|
||||
<LinearLayout
|
||||
android:id="@+id/layoutButtonsHorizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="100dp"
|
||||
android:gravity="center_vertical"
|
||||
@@ -69,4 +71,51 @@
|
||||
android:textStyle="bold" />
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 3个按钮时显示(垂直排列,iOS风格) -->
|
||||
<LinearLayout
|
||||
android:id="@+id/layoutButtonsVertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:visibility="gone">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/btnTop"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="100dp"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/ripple_effect_light2"
|
||||
android:textColor="@color/dish_green"
|
||||
android:textSize="30sp" />
|
||||
|
||||
<com.google.android.material.divider.MaterialDivider
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="@color/gray_eb" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/btnMiddle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="100dp"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/ripple_effect_light2"
|
||||
android:textColor="@color/dish_green"
|
||||
android:textSize="30sp" />
|
||||
|
||||
<com.google.android.material.divider.MaterialDivider
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="@color/gray_eb" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/btnBottom"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="100dp"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/ripple_effect_light2"
|
||||
android:textColor="@color/dish_green"
|
||||
android:textSize="30sp"
|
||||
android:textStyle="bold" />
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user