添加退出二次确认
MoMo Lv5

新建Fragment

res/layout/dialog_confirm_logout.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>

</data>

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="#CFCFCF">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="@dimen/app_dialog_width"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:background="#FFFFFF">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/confirm_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="确认退出?"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/confirm_accept"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="16dp"
android:background="@drawable/bg_rounded_rect"
android:backgroundTint="@color/app_primary"
android:focusable="true"
android:gravity="center"
android:paddingHorizontal="16dp"
android:text="确定"
android:textColor="@color/app_std_text_white"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/confirm_title" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/confirm_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="20dp"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="24dp"
android:alpha="0.45"
android:gravity="center"
android:text="取消"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/confirm_accept" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

image

添加Fragment绑定

com/danale/edge/ui/common/dialog/AcceptConfirmLogoutFragment.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.danale.edge.ui.common.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.danale.edge.R

// Fragment
import com.danale.edge.databinding.DialogConfirmLogoutBinding

class AcceptConfirmLogoutFragment (val callback: (Int) -> Unit) : DialogFragment() {
companion object {
// 定义两种操作
const val ACTION_CONFIRM_LOGOUT = 9001
const val ACTION_CANCEL_LOGOUT = 9002
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return AcceptConfirmLogout(
callback,
requireContext(),
R.style.Theme_ViewWorld_Dialog
).apply {
window?.setBackgroundDrawableResource(R.drawable.bg_rounded_rect)
}
}

class AcceptConfirmLogout(
val callback: (Int) -> Unit,
context: Context,
themeResId: Int
) : Dialog(context, themeResId) {

val binding: DialogConfirmLogoutBinding // binding

init {
binding = DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.dialog_confirm_logout,
null,
false
)

// 确认退出
binding.confirmAccept.setOnClickListener {
dismiss()
callback(ACTION_CONFIRM_LOGOUT)
}

// 取消退出
binding.confirmCancel.setOnClickListener {
dismiss()
callback(ACTION_CANCEL_LOGOUT)
}

setContentView(binding.root)
}

}

}

二次确认

com/danale/edge/ui/user/MyInfoFragment.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import com.danale.edge.ui.common.dialog.AcceptConfirmLogoutFragment


// 退出登录
binding.groupLogout.setOnClickListener {
try {
AcceptConfirmLogoutFragment {
when (it) {
AcceptConfirmLogoutFragment.ACTION_CONFIRM_LOGOUT -> {
lifecycleScope.launch {
viewModel.logout() // 退出
}
}
AcceptConfirmLogoutFragment.ACTION_CANCEL_LOGOUT -> {
return@AcceptConfirmLogoutFragment
}
}
}.show(parentFragmentManager, "confirm_logout")

} catch (e: Exception) {
toastError(e)
}
}

logout是suspend异步函数,需要另起一个协程,写在launch

Powered by Hexo & Theme Keep
Unique Visitor Page View