回答編集履歴
2
コード修正忘れ
test
CHANGED
@@ -40,8 +40,6 @@
|
|
40
40
|
|
41
41
|
private lateinit var viewBinding: ActivityMainBinding
|
42
42
|
|
43
|
-
val myDialogFragment = MyDialogFragment()
|
44
|
-
|
45
43
|
|
46
44
|
|
47
45
|
override fun onCreate(savedInstanceState: Bundle?) {
|
1
コード追加
test
CHANGED
@@ -3,3 +3,223 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
フラグメントは一般的にシステムに渡した後はアプリで参照を保持しないほうが良いです。表示が終わると自動で削除される(ことがある)為、再利用は出来ず、その参照を保持しているとメモリリークとなります。(正式に具体的にどこでどうなって…という所は追っていないのでお伝え出来ません(_ _;スミマセン )
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
----
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
ダイアログで 2 つの EditText の入力をして OK を押したらインターフェースのメソッドを呼ぶようにしてみました。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
MainActivity.kt
|
18
|
+
|
19
|
+
```kotlin
|
20
|
+
|
21
|
+
package com.teratail.q365939
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
import androidx.appcompat.app.AppCompatActivity
|
26
|
+
|
27
|
+
import android.os.Bundle
|
28
|
+
|
29
|
+
import android.util.Log
|
30
|
+
|
31
|
+
import com.teratail.q365939.databinding.ActivityMainBinding
|
32
|
+
|
33
|
+
//import net.sytes.rokkosan.myalertdialog.databinding.ActivityMainBinding
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
class MainActivity : AppCompatActivity(), MyDialogFragment.DialogListener {
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
private lateinit var viewBinding: ActivityMainBinding
|
42
|
+
|
43
|
+
val myDialogFragment = MyDialogFragment()
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
48
|
+
|
49
|
+
super.onCreate(savedInstanceState)
|
50
|
+
|
51
|
+
//setContentView(R.layout.activity_main)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
// set View Binding
|
56
|
+
|
57
|
+
viewBinding = ActivityMainBinding.inflate(layoutInflater)
|
58
|
+
|
59
|
+
setContentView(viewBinding.root)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
viewBinding.btnOpenDialog.setOnClickListener {
|
64
|
+
|
65
|
+
val myDialogFragment = MyDialogFragment()
|
66
|
+
|
67
|
+
myDialogFragment.show(supportFragmentManager, "my")
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
override fun onMyDialogOk(text1: String, text2: String) {
|
76
|
+
|
77
|
+
Log.d("MainActivity", "text1=$text1")
|
78
|
+
|
79
|
+
Log.d("MainActivity", "text2=$text2")
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
MyDialogFragment.kt
|
88
|
+
|
89
|
+
```kotlin
|
90
|
+
|
91
|
+
package com.teratail.q365939
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
import android.app.Dialog
|
96
|
+
|
97
|
+
import android.content.Context
|
98
|
+
|
99
|
+
import android.os.Bundle
|
100
|
+
|
101
|
+
import android.text.Editable
|
102
|
+
|
103
|
+
import android.text.TextWatcher
|
104
|
+
|
105
|
+
import android.widget.Button
|
106
|
+
|
107
|
+
import android.widget.EditText
|
108
|
+
|
109
|
+
import androidx.appcompat.app.AlertDialog
|
110
|
+
|
111
|
+
import androidx.fragment.app.DialogFragment
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
//[ダイアログ](https://developer.android.com/guide/topics/ui/dialogs?hl=ja)
|
116
|
+
|
117
|
+
class MyDialogFragment: DialogFragment() {
|
118
|
+
|
119
|
+
internal lateinit var listener: DialogListener
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
interface DialogListener {
|
124
|
+
|
125
|
+
fun onMyDialogOk(text1: String, text2: String)
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
override fun onAttach(context: Context) {
|
132
|
+
|
133
|
+
super.onAttach(context)
|
134
|
+
|
135
|
+
try {
|
136
|
+
|
137
|
+
listener = context as DialogListener
|
138
|
+
|
139
|
+
} catch (e: ClassCastException) {
|
140
|
+
|
141
|
+
throw ClassCastException((context.toString() + " must implement DialogListener"))
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
//二つの EditText に文字列があるか無いかで Button の enable を制御する
|
150
|
+
|
151
|
+
class EditsWatcher(val button: Button, val edit1: EditText, val edit2: EditText): TextWatcher {
|
152
|
+
|
153
|
+
init {
|
154
|
+
|
155
|
+
edit1.addTextChangedListener(this)
|
156
|
+
|
157
|
+
edit2.addTextChangedListener(this)
|
158
|
+
|
159
|
+
afterTextChanged(null)
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} //ignore
|
164
|
+
|
165
|
+
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} //ignore
|
166
|
+
|
167
|
+
override fun afterTextChanged(s: Editable?) {
|
168
|
+
|
169
|
+
button.isEnabled = edit1.text.toString().trim().isNotEmpty() && edit2.text.toString().trim().isNotEmpty();
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
178
|
+
|
179
|
+
return activity?.let {
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
val view = requireActivity().layoutInflater.inflate(R.layout.my_dialog, null)
|
184
|
+
|
185
|
+
val edData1 = view.findViewById<EditText>(R.id.etData1)
|
186
|
+
|
187
|
+
val edData2 = view.findViewById<EditText>(R.id.etData2)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
val builder = AlertDialog.Builder(it).setView(view)
|
192
|
+
|
193
|
+
.setPositiveButton(android.R.string.ok, { dialog, id ->
|
194
|
+
|
195
|
+
listener.onMyDialogOk(edData1.text.toString(), edData2.text.toString())
|
196
|
+
|
197
|
+
})
|
198
|
+
|
199
|
+
.setNegativeButton(android.R.string.cancel, { dialog, id ->
|
200
|
+
|
201
|
+
dialog.cancel()
|
202
|
+
|
203
|
+
})
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
builder.create().also { dialog ->
|
208
|
+
|
209
|
+
dialog.setOnShowListener({
|
210
|
+
|
211
|
+
val button = dialog.getButton(Dialog.BUTTON_POSITIVE)
|
212
|
+
|
213
|
+
EditsWatcher(button, edData1, edData2)
|
214
|
+
|
215
|
+
})
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
} ?: throw IllegalStateException("Activity cannot be null")
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
```
|