-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSampleActivity.kt
69 lines (56 loc) · 2.2 KB
/
SampleActivity.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
65
66
67
68
69
package com.example
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.vlad.android.kotlin.*
class SampleActivity: Activity() {
private val ACTION_HELLO = "com.example.intent.action.HELLO"
private val DIALOG_EXAMPLE = 1
public override fun onCreate(savedInstanceState: Bundle?) {
super<Activity>.onCreate(savedInstanceState)
setContentView(R.layout.main)
val buttonHello = findView<Button>(R.id.button_hello)
val edittext = findView<EditText>(R.id.edittext)
buttonHello?.setOnClickListener { showDialog(DIALOG_EXAMPLE) }
edittext?.setOnEditorActionListener { v, actionId, event ->
when (actionId) {
EditorInfo.IME_NULL -> {
getInputMethodService()?.hideSoftInputFromWindow(edittext?.getWindowToken(), 0)
Toast.makeText(this, "Enter", Toast.LENGTH_SHORT)?.show()
true
}
else -> false
}
}
registerReceiver(broadcastReceiver, IntentFilter {
addAction(ACTION_HELLO)
})
}
protected override fun onDestroy() {
super<Activity>.onDestroy()
unregisterReceiver(broadcastReceiver)
}
private val broadcastReceiver = BroadcastReceiver { context, intent ->
Toast.makeText(context,
when (intent?.getAction()) {
ACTION_HELLO -> "Hello world!"
else -> "Unknown action"
}, Toast.LENGTH_LONG)?.show()
}
public override fun onCreateDialog(id: Int) = when (id) {
DIALOG_EXAMPLE -> AlertDialog.Builder(this).setTitle("Title")
?.setMessage("Want to say hello to world?")
?.setPositiveButton("Yes", { dialog, which -> sendBroadcast(ACTION_HELLO.toIntent()) })
?.setNegativeButton("No", { dialog, which -> })
?.create()
else -> super.onCreateDialog(id)
}
}