Kotlin DialogFragment editText editable always null

℡╲_俬逩灬. 提交于 2021-01-28 06:55:19

问题


So, I'm using Kotlin extensions which is straightforward, but I can't get string from edittext

here is my code:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    val v = activity.layoutInflater
            .inflate(R.layout.dialog_group, null)

    v.add_group_button.setOnClickListener(addListener)

    return AlertDialog.Builder(activity)
            .setView(v)
            .create()
}

private var addListener: View.OnClickListener = View.OnClickListener {

        val groupNameInput: String = view?.group_edit_text?.text.toString()

    }

when I'm pressing add button groupNameInput always returns null why?


回答1:


So finally I figure it out — on Dialog Fragment view will always be null, because its never created, but it's created and added to dialog view which means I need to call:

dialog.group_edit_text.text.toString()

Instead of:

view.group_edit_text.text.toString()


来源:https://stackoverflow.com/questions/47285180/kotlin-dialogfragment-edittext-editable-always-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!