回答編集履歴
2
変更
test
CHANGED
@@ -1 +1,138 @@
|
|
1
|
+
kotlin は不慣れなものでこちらも試行錯誤ですが、一応動作しています。
|
2
|
+
|
3
|
+
MainActivity.kt
|
4
|
+
```kotlin
|
5
|
+
import android.icu.util.Calendar
|
6
|
+
import androidx.appcompat.app.AppCompatActivity
|
7
|
+
import android.os.Bundle
|
8
|
+
import android.os.Handler
|
9
|
+
import android.os.Looper
|
10
|
+
import android.widget.Button
|
11
|
+
import android.widget.TextView
|
12
|
+
import androidx.appcompat.app.AlertDialog
|
13
|
+
|
14
|
+
class MainActivity : AppCompatActivity() {
|
1
|
-
|
15
|
+
companion object {
|
16
|
+
const val REQUESTKEY_TIMEPICKER = "selectTime"
|
17
|
+
}
|
18
|
+
|
19
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
20
|
+
super.onCreate(savedInstanceState)
|
21
|
+
setContentView(R.layout.activity_main)
|
22
|
+
|
23
|
+
val nowText = findViewById<TextView>(R.id.nowText)
|
24
|
+
val alarmText = findViewById<TextView>(R.id.alarmText)
|
25
|
+
|
26
|
+
supportFragmentManager.setFragmentResultListener(REQUESTKEY_TIMEPICKER, this, { requestLKey, result ->
|
27
|
+
val hour = result.getInt(TimePickerDialogFragment.RESULT_HOUR)
|
28
|
+
val minute = result.getInt(TimePickerDialogFragment.RESULT_MINUTE)
|
29
|
+
alarmText.text = "%d時%2d分".format(hour, minute)
|
30
|
+
})
|
31
|
+
|
32
|
+
val btnSelectTime = findViewById<Button>(R.id.btnSelectTime)
|
33
|
+
btnSelectTime.setOnClickListener {
|
34
|
+
TimePickerDialogFragment.getInstance(REQUESTKEY_TIMEPICKER).show(supportFragmentManager, null)
|
35
|
+
}
|
36
|
+
|
37
|
+
val handler = Handler(Looper.getMainLooper())
|
38
|
+
class TimeDrawer : Runnable {
|
39
|
+
override fun run() {
|
40
|
+
val calendar = Calendar.getInstance()
|
41
|
+
val hour = calendar.get(Calendar.HOUR_OF_DAY)
|
42
|
+
val minute = calendar.get(Calendar.MINUTE)
|
43
|
+
val second = calendar.get(Calendar.SECOND)
|
44
|
+
nowText.text = "%d時%2d分".format(hour, minute)
|
45
|
+
if (alarmText.text == nowText.text) {
|
46
|
+
AlertDialog.Builder(this@MainActivity)
|
47
|
+
.setTitle("時間になりました!")
|
48
|
+
.setPositiveButton("Hello", null)
|
49
|
+
.show()
|
50
|
+
}
|
51
|
+
handler.postDelayed(this, (60 - second) * 1000L)
|
52
|
+
}
|
53
|
+
}
|
54
|
+
handler.post(TimeDrawer())
|
55
|
+
}
|
56
|
+
}
|
57
|
+
```
|
58
|
+
res/layout/activity_main.xml
|
59
|
+
```xml
|
60
|
+
<?xml version="1.0" encoding="utf-8"?>
|
61
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
62
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
63
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
64
|
+
xmlns:tools="http://schemas.android.com/tools"
|
65
|
+
android:layout_width="match_parent"
|
66
|
+
android:layout_height="match_parent"
|
67
|
+
tools:context=".MainActivity">
|
68
|
+
|
69
|
+
<TextView
|
70
|
+
android:id="@+id/alarmText"
|
71
|
+
android:layout_width="wrap_content"
|
72
|
+
android:layout_height="wrap_content"
|
73
|
+
android:text="Hello World!"
|
74
|
+
app:layout_constraintBottom_toTopOf="@id/nowText"
|
75
|
+
app:layout_constraintEnd_toEndOf="parent"
|
76
|
+
app:layout_constraintStart_toStartOf="parent"
|
77
|
+
app:layout_constraintTop_toTopOf="parent" />
|
78
|
+
<TextView
|
79
|
+
android:id="@+id/nowText"
|
80
|
+
android:layout_width="wrap_content"
|
81
|
+
android:layout_height="wrap_content"
|
82
|
+
android:text="Hello World!"
|
83
|
+
app:layout_constraintBottom_toTopOf="@id/btnSelectTime"
|
84
|
+
app:layout_constraintEnd_toEndOf="parent"
|
85
|
+
app:layout_constraintStart_toStartOf="parent"
|
86
|
+
app:layout_constraintTop_toBottomOf="@id/alarmText" />
|
87
|
+
<Button
|
88
|
+
android:id="@+id/btnSelectTime"
|
89
|
+
android:layout_width="wrap_content"
|
90
|
+
android:layout_height="wrap_content"
|
91
|
+
android:text="SELECT TIME"
|
92
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
93
|
+
app:layout_constraintEnd_toEndOf="parent"
|
94
|
+
app:layout_constraintStart_toStartOf="parent"
|
95
|
+
app:layout_constraintTop_toBottomOf="@id/nowText" />
|
96
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
97
|
+
```
|
98
|
+
TimePickerDialogFragment.kt
|
99
|
+
```kotlin
|
100
|
+
import android.app.Dialog
|
101
|
+
import android.app.TimePickerDialog
|
102
|
+
import android.os.Bundle
|
103
|
+
import androidx.fragment.app.DialogFragment
|
104
|
+
import java.util.*
|
105
|
+
|
106
|
+
class TimePickerDialogFragment private constructor() : DialogFragment() {
|
107
|
+
companion object {
|
108
|
+
@Suppress("unused")
|
109
|
+
private val TAG = TimePickerDialogFragment::class.java.simpleName
|
110
|
+
|
111
|
+
private const val ARGS_REQUESTKEY = "requestKey"
|
112
|
+
const val RESULT_HOUR = "hour"
|
113
|
+
const val RESULT_MINUTE = "minute"
|
114
|
+
|
115
|
+
fun getInstance(requestKey: String) : TimePickerDialogFragment {
|
116
|
+
return TimePickerDialogFragment().apply {
|
117
|
+
arguments = Bundle().apply {
|
118
|
+
putString(ARGS_REQUESTKEY, requestKey)
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
125
|
+
val requestKey = arguments?.getString(ARGS_REQUESTKEY)
|
126
|
+
if(requestKey == null) throw IllegalArgumentException("no ${ARGS_REQUESTKEY} in args")
|
127
|
+
|
128
|
+
val calendar = Calendar.getInstance()
|
129
|
+
return TimePickerDialog(context, { timePicker, hour, minute ->
|
130
|
+
parentFragmentManager.setFragmentResult(requestKey, Bundle().apply {
|
131
|
+
putInt(RESULT_HOUR, hour)
|
132
|
+
putInt(RESULT_MINUTE, minute)
|
133
|
+
})
|
134
|
+
}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true)
|
135
|
+
}
|
136
|
+
}
|
137
|
+
```
|
138
|
+

|
1
おっと
test
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
とりあえず、```timeTextView.text==tvTime.text``` では比較出来ないです。
|
2
|
-
|
1
|
+
(java じゃなかったですスミマセン)
|