Created
January 21, 2020 09:57
-
-
Save Akhu/d25bc1cf96cebaa52011aaa4e7179b44 to your computer and use it in GitHub Desktop.
Dialog Fragment with closure to get the content of EditText
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RegisterDialogFragment : DialogFragment() { | |
| var onPositiveClick: ((userName: String) -> Unit)? = null | |
| var onNegativeClick: (() -> Unit)? = null | |
| override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
| val editText = EditText(context) | |
| editText.inputType = InputType.TYPE_CLASS_TEXT | |
| return with(AlertDialog.Builder(context)) { | |
| setView(editText) | |
| setTitle("Create a new Profile") | |
| setMessage("Enter an astronaut Pseudo") | |
| setPositiveButton("Register") { _, _ -> onPositiveClick?.invoke(editText.text.toString()) } | |
| setNegativeButton("Cancel") { dialog, _ -> | |
| dialog.cancel() | |
| onNegativeClick?.invoke() } | |
| create() | |
| } | |
| } | |
| } | |
| // Usage | |
| fun showDialog() { | |
| val dialog = RegisterDialogFragment() | |
| dialog.onPositiveClick = { | |
| val myEditTextValue = it | |
| } | |
| activity?.supportFragmentManager?.let { dialog.show(it, "RegisterDialog") } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment