Commit 4250d78b authored by zhaopengxiang's avatar zhaopengxiang

修复弹窗点击button时的闪烁问题,抽取common弹窗

parent d9aa2ef1
......@@ -3,6 +3,7 @@ package com.miya.fastcashier.ui
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
......@@ -11,6 +12,7 @@ import com.miya.fastcashier.databinding.ActivityLoginBinding
import com.miya.fastcashier.databinding.ActivityMainBinding
import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.service.PrintService
import com.miya.fastcashier.ui.dialog.CommonDialog
import com.miya.fastcashier.ui.dialog.MessageDialog
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.DateSelectHelper
......@@ -24,7 +26,8 @@ import java.util.*
class MainActivity : BaseActivity() {
//退出登录 二次确认弹窗
private var alertDialog: MessageDialog? = null
// private var alertDialog: MessageDialog? = null
private var alertDialog: CommonDialog? = null
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: MainViewModel
private var dateSelectHelper: DateSelectHelper? = null
......@@ -124,23 +127,34 @@ class MainActivity : BaseActivity() {
}
private fun loginOut() {
if (null == alertDialog) {
alertDialog = MessageDialog(this)
.setCustomMessage("是否确认退出登录账号")
.setPositiveButton("确认退出") { _, _ ->
run {
MMKV.defaultMMKV().clearAll()
AccountService.setAccountInfo(null)
val intent = Intent()
intent.setClass(this@MainActivity, LoginActivity::class.java)
startActivity(intent)
finish()
if (alertDialog == null || !alertDialog!!.isShowing) {
alertDialog = CommonDialog(this)
alertDialog!!.setCustomMessage(getString(R.string.app_text_login_out))
.setNegativeStr(getString(R.string.cancel))
.setPositiveStr(getString(R.string.text_login_out_sure))
.setMessageGravity(Gravity.CENTER_HORIZONTAL)
.setOnClickListener(object : CommonDialog.OnDialogClickListener {
override fun onNegativeClick() {
alertDialog!!.dismiss()
alertDialog = null
}
}
override fun onPositiveClick() {
alertDialog!!.dismiss()
alertDialog = null
run {
MMKV.defaultMMKV().clearAll()
AccountService.setAccountInfo(null)
val intent = Intent()
intent.setClass(this@MainActivity,
LoginActivity::class.java)
startActivity(intent)
finish()
}
}
})
.show()
}
alertDialog?.show()
}
override fun onDestroy() {
......
package com.miya.fastcashier.ui.dialog
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.widget.TextView
import android.text.SpannableString
import android.view.Gravity
import com.miya.fastcashier.R
import android.os.Bundle
import android.text.TextUtils
import android.view.ViewGroup.MarginLayoutParams
import android.text.method.LinkMovementMethod
import android.view.View
import com.miya.fastcashier.utils.clickWithTrigger
class CommonDialog : Dialog {
protected var tvMessage: TextView? = null
protected var tvNegative: TextView? = null
protected var tvPositive: TextView? = null
protected var onClickListener: OnDialogClickListener? = null
protected var mContext: Context
private var mCustomMessage = ""
private var mContentSpanStr: SpannableString? = null
private var mPositiveStr = ""
private var mNegativeStr = ""
private var mMessageGravity = Gravity.LEFT
private var contentLeftMargin = 0
private var contentRightMargin = 0
private var contentTopMargin = 0
private var contentBottomMargin = 0
interface OnDialogClickListener {
fun onNegativeClick()
fun onPositiveClick()
}
fun setOnClickListener(onClickListener: OnDialogClickListener?): CommonDialog {
this.onClickListener = onClickListener
return this
}
constructor(ctx: Context, theme: Int) : super(ctx, theme) {
setCanceledOnTouchOutside(false)
setCancelable(false)
mContext = ctx
}
constructor(ctx: Context) : super(ctx, R.style.CommonDialog) {
setCanceledOnTouchOutside(false)
setCancelable(false)
mContext = ctx
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.setContentView(R.layout.dialog_common)
tvMessage = findViewById(R.id.tvMessage)
tvNegative = findViewById(R.id.tvNegative)
tvPositive = findViewById(R.id.tvPositive)
tvNegative?.clickWithTrigger {
if (onClickListener != null) {
onClickListener!!.onNegativeClick()
}
}
tvPositive?.clickWithTrigger {
if (onClickListener != null) {
onClickListener!!.onPositiveClick()
}
}
if (!TextUtils.isEmpty(mCustomMessage)) {
tvMessage?.setText(mCustomMessage)
val marginLayoutParams = tvMessage?.getLayoutParams() as MarginLayoutParams
if (contentTopMargin != 0) {
marginLayoutParams.topMargin = contentTopMargin
}
if (contentBottomMargin != 0) {
marginLayoutParams.bottomMargin = contentBottomMargin
}
if (contentLeftMargin != 0) {
marginLayoutParams.leftMargin = contentLeftMargin
}
if (contentRightMargin != 0) {
marginLayoutParams.rightMargin = contentRightMargin
}
tvMessage?.getViewTreeObserver()?.addOnGlobalLayoutListener {
val textPaint = tvMessage?.getPaint()
textPaint?.textSize = tvMessage!!.getTextSize()
val textViewWidth = textPaint!!.measureText(mCustomMessage).toInt()
tvMessage?.setText(mCustomMessage)
if (textViewWidth > tvMessage!!.getWidth()) {
tvMessage?.setGravity(Gravity.LEFT)
} else {
tvMessage?.setGravity(Gravity.CENTER)
}
}
} else {
if (mContentSpanStr != null) {
tvMessage?.setText(mContentSpanStr)
tvMessage?.setMovementMethod(LinkMovementMethod.getInstance())
tvMessage?.setHighlightColor(Color.TRANSPARENT)
val marginLayoutParams = tvMessage?.getLayoutParams() as MarginLayoutParams
if (contentTopMargin != 0) {
marginLayoutParams.topMargin = contentTopMargin
}
if (contentBottomMargin != 0) {
marginLayoutParams.bottomMargin = contentBottomMargin
}
if (contentLeftMargin != 0) {
marginLayoutParams.leftMargin = contentLeftMargin
}
if (contentRightMargin != 0) {
marginLayoutParams.rightMargin = contentRightMargin
}
}
tvMessage?.setGravity(mMessageGravity)
}
if (!TextUtils.isEmpty(mPositiveStr)) {
tvPositive?.setText(mPositiveStr)
}
if (!TextUtils.isEmpty(mNegativeStr)) {
tvNegative?.setText(mNegativeStr)
}
resize()
}
fun setCustomMessage(message: String): CommonDialog {
mCustomMessage = message
return this
}
fun setMessageGravity(gravity: Int): CommonDialog {
mMessageGravity = gravity
return this
}
fun setPositiveStr(positiveStr: String): CommonDialog {
mPositiveStr = positiveStr
return this
}
fun setNegativeStr(negativeStr: String): CommonDialog {
mNegativeStr = negativeStr
return this
}
fun setTvMessageMargin(
leftMargin: Int,
rightMargin: Int,
topMargin: Int,
bottomMargin: Int
): CommonDialog {
contentLeftMargin = leftMargin
contentRightMargin = rightMargin
contentTopMargin = topMargin
contentBottomMargin = bottomMargin
return this
}
fun show(gravity: Int) {
this.window!!.setGravity(gravity)
this.show()
}
fun resize() {
this.window!!.setLayout(-1, -2)
}
}
\ No newline at end of file
<?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:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:background="@drawable/app_center_dialog_bg"
android:layout_width="match_parent"
android:layout_marginStart="85dp"
android:layout_marginEnd="85dp"
android:layout_marginRight="85dp"
android:layout_height="264dp"
android:layout_marginLeft="85dp">
<TextView
android:id="@+id/tvMessage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#323233"
android:textSize="28sp"
tools:text="是否确认退款"
android:layout_marginTop="61dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:id="@+id/vHorizontalLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="164dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#E8E8E8" />
<View
android:id="@+id/vVerticalLine"
android:layout_width="1dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vHorizontalLine"
android:background="#E8E8E8" />
<TextView
android:id="@+id/tvNegative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:textColor="#969799"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/vVerticalLine"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vHorizontalLine" />
<TextView
android:id="@+id/tvPositive"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/vVerticalLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vHorizontalLine"
android:textColor="#111235"
android:textSize="28sp"
android:text="确定"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -21,5 +21,7 @@
<string name="cancel">取消</string>
<string name="sure">确定</string>
<string name="search_order_prompt_text">还没有明细哦~</string>
<string name="app_text_login_out">是否确认退出登录账号</string>
<string name="text_login_out_sure">确认退出</string>
</resources>
\ No newline at end of file
......@@ -23,4 +23,14 @@
<item name="android:lines">1</item>
<item name="android:maxLines">1</item>
</style>
<style name="CommonDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.7</item>
</style>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment