为什么Dialog在Kotlin中不显示?

时间:2019-10-09 01:51:07

标签: android kotlin dialog floating-action-button

当我单击浮动动作按钮时,我想创建一个对话框窗口。但是,当我单击按钮时,仅显示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()后,它的工作正常。谢谢。

4 个答案:

答案 0 :(得分:2)

您忘记了在对话框上调用show()。 dialog.show()

答案 1 :(得分:0)

您需要调用dialog.show()。只需在dialog.setCancelable(false)下的任何位置调用它即可。

答案 2 :(得分:0)

您必须像以下那样调用show()

//...
dialog.setTitle("Add person")
dialog.setCancelable(false)
// ...
btnAdd.setOnClickListener{
    //...
}
btnCancel.setOnClickListener {
    dialog.dismiss()
}

//show dialog adding below line.
dialog.show();

答案 3 :(得分:0)

创建对话框后,我们需要调用show方法在屏幕上显示对话框

在创建对话框dialog.show()后添加此行

请替换或添加此代码

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()
        }
    //add this line 
     //Call show() method to show dialog 
  dialog.show()
    }
}