【问题标题】:why Dialog doesn't show in Kotlin?为什么 Dialog 在 Kotlin 中不显示?
【发布时间】: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


    【解决方案1】:

    您忘记在对话框中调用 show()。 对话框.show()

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        创建对话框后,我们需要调用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()
            }
        }
        

        【讨论】:

          【解决方案4】:

          你必须像下面这样调用show()

          //...
          dialog.setTitle("Add person")
          dialog.setCancelable(false)
          // ...
          btnAdd.setOnClickListener{
              //...
          }
          btnCancel.setOnClickListener {
              dialog.dismiss()
          }
          
          //show dialog adding below line.
          dialog.show();
          

          【讨论】:

            猜你喜欢
            • 2020-12-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多