【发布时间】:2019-10-09 01:51:07
【问题描述】:
我想在单击浮动操作按钮时创建一个对话框窗口。但是,当我单击按钮时,只会出现 Toast 消息。
这是我迄今为止尝试过的:
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
val users = ArrayList<User>()
users.add(User("John", "USA"))
val adapter = CustomAdapter(users)
recyclerView.adapter = adapter
fab.setOnClickListener {
val dialog = Dialog(this)
Toast.makeText(this, "It's working...", Toast.LENGTH_LONG).show()
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(R.layout.dialog_add)
dialog.setTitle("Add person")
dialog.setCancelable(false)
val nameText = dialog.findViewById(R.id.name) as EditText
val addressText = dialog.findViewById(R.id.address) as EditText
val btnAdd = dialog.findViewById(R.id.btn_ok) as Button
val btnCancel = dialog.findViewById(R.id.btn_cancel) as Button
btnAdd.setOnClickListener{
users.add(User(nameText.text.toString(), addressText.text.toString()))
adapter.notifyDataSetChanged()
dialog.dismiss()
}
btnCancel.setOnClickListener {
dialog.dismiss()
}
}
}
如何更改代码,以便在单击 FAB 时显示对话窗口?
更新: 你们是对的!在我放置 dialog.show() 后它工作得很好。谢谢。
【问题讨论】:
标签: android kotlin dialog floating-action-button