1
- package com.zcy.nidavellir. fancydialog
1
+ package com.zcy.fancydialog
2
2
3
+ import android.content.Context
3
4
import android.os.Bundle
4
5
import android.os.Handler
5
6
import android.os.Looper
@@ -8,7 +9,8 @@ import android.text.TextWatcher
8
9
import android.view.LayoutInflater
9
10
import android.view.View
10
11
import android.view.ViewGroup
11
- import android.widget.Toast
12
+ import android.view.WindowManager
13
+ import android.view.inputmethod.InputMethodManager
12
14
import androidx.appcompat.widget.AppCompatEditText
13
15
import androidx.appcompat.widget.AppCompatTextView
14
16
import androidx.fragment.app.FragmentManager
@@ -18,7 +20,6 @@ import androidx.fragment.app.FragmentManager
18
20
* @description: 带输入框的Dialog
19
21
* @date: 2019/1/2
20
22
*/
21
-
22
23
inline fun editDialog (fragmentManager : FragmentManager , dsl : DSLEditDialog .() -> Unit ) {
23
24
DSLEditDialog .newInstance().apply (dsl).show(fragmentManager, " dialog" )
24
25
}
@@ -40,9 +41,16 @@ class DSLEditDialog : BaseFragmentDialog() {
40
41
private var leftClicks: (() -> Unit )? = null
41
42
private var rightClicks: ((String ) -> Unit )? = null
42
43
44
+ private lateinit var rootView: View
45
+
46
+ init {
47
+ lowerBackground = true
48
+ }
49
+
43
50
private lateinit var edit: AppCompatEditText
44
51
override fun setView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
45
52
val view = inflater.inflate(R .layout.layout_edit_dialog, container, false )
53
+ rootView = view
46
54
edit = view.findViewById<AppCompatEditText >(R .id.labelEdit)
47
55
edit.setText(mDefault)
48
56
mHint?.let {
@@ -53,9 +61,7 @@ class DSLEditDialog : BaseFragmentDialog() {
53
61
val sure = view.findViewById<AppCompatTextView >(R .id.sure)
54
62
val title = view.findViewById<AppCompatTextView >(R .id.title)
55
63
val message = view.findViewById<AppCompatTextView >(R .id.desc)
56
- mTitle?.also {
57
- title.text = mTitle
58
- }
64
+ title.text = mTitle
59
65
mMessage?.let {
60
66
message.isVisibility(true )
61
67
message.text = it
@@ -77,13 +83,12 @@ class DSLEditDialog : BaseFragmentDialog() {
77
83
sure?.isEnabled = true
78
84
sure?.alpha = 1F
79
85
if (s.length > mMaxLength) {
80
- Toast .makeText(context, " 太多了" , Toast .LENGTH_SHORT ).show()
81
- edit?.let {
86
+ edit.let {
82
87
val selectionStart = it.selectionStart
83
88
val selectionEnd = it.selectionEnd
84
89
s.delete(selectionStart - 1 , selectionEnd)
85
90
it.text = s
86
- it.setSelection(it.text!! .length)
91
+ it.setSelection(it.text.toString() .length)
87
92
return @TAG
88
93
}
89
94
}
@@ -105,7 +110,7 @@ class DSLEditDialog : BaseFragmentDialog() {
105
110
106
111
sure.setOnClickListener {
107
112
rightClicks?.let { onClick ->
108
- onClick(edit.text!! .trim ().toString ())
113
+ onClick(edit.text.toString ().trim ())
109
114
}
110
115
dismiss()
111
116
}
@@ -127,12 +132,24 @@ class DSLEditDialog : BaseFragmentDialog() {
127
132
rightClicks = onClick
128
133
}
129
134
135
+ override fun dismiss () {
136
+ Handler (Looper .getMainLooper()).post { hintPopInput(rootView) }
137
+ super .dismiss()
138
+ }
139
+
140
+ private fun hintPopInput (view : View ) {
141
+ val imm = mContext.getSystemService(Context .INPUT_METHOD_SERVICE ) as ? InputMethodManager
142
+ imm?.hideSoftInputFromWindow(view.windowToken, 2 )
143
+ activity?.window?.setSoftInputMode(WindowManager .LayoutParams .SOFT_INPUT_STATE_ALWAYS_HIDDEN )
144
+ }
145
+
146
+
130
147
override fun show (manager : FragmentManager , tag : String? ) {
131
148
Handler (Looper .getMainLooper()).postDelayed({
132
149
if (this ::edit.isInitialized) {
133
150
edit.showSoft()
134
151
}
135
- }, 200 )
152
+ }, 100 )
136
153
super .show(manager, tag)
137
154
}
138
155
0 commit comments