Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

android - How can I call Toast in Dialog? - kotlin

When I write a code in dialog :

Toast.makeText(this,"This is a toast", Toast.LENGTH_LONG).show()

It always shows error on this and is not compiled. What should I use instead of this, when I want to call it in dialog ?

My whole code for dialog is :

class write_text (context: Context) {
private val dlg_write = Dialog(context)
lateinit var title : EditText
lateinit var content : EditText
lateinit var submittext:ImageButton
fun start(boardnum:Int,id_board:String)
{
    dlg_write.setCanceledOnTouchOutside(false) // Cancel Button ? ??? ?.
    dlg_write.setContentView(R.layout.activity_write_text)
    val mAuth = FirebaseAuth.getInstance()
    val uid = mAuth.currentUser?.uid.toString()
    title = dlg_write.findViewById<EditText>(R.id.write_title)
    content = dlg_write.findViewById<EditText>(R.id.write_content)
    val time_write = LocalDateTime.now()
    val time_charonly = time_write.toString().split(":",".")
    var time_for_id = ""
    for(tc in time_charonly)
    {
        time_for_id+=tc
    }

    submittext = dlg_write.findViewById(R.id.submit_text)

    submittext.setOnClickListener {
        val board_id = uid+"_text"+boardnum.toString()

        val database = FirebaseDatabase.getInstance()
        val ref_board = database.getReference(uid).child(board_id) // ?? ?? ???? ? id
        val text_id = time_for_id+"_"+uid+boardnum.toString()
        var title_text = ""
        if(title.text.toString() ==""&& content.text.toString() =="")
        {
            Toast.makeText(getContext(),"Empty contents",Toast.LENGTH_LONG).show()
            return@setOnClickListener
        }
        if (title.text.toString() =="")
        {
            title_text = "Noname"
        }
        val text_written = textboard_contents(
                //text_id = text_id,
                title = title.text.toString(),
                contents = content.text.toString(),
                time = time_write.toString(),
                roomofthistext = boardnum,
                num_of_like = 0,
                num_of_comment = 0
        )
        ref_board.child(text_id).setValue(text_written)
        //val content_id =

// val send_back = Intent(board_text_owner) return@setOnClickListener

        dlg_write.dismiss()
    }
    dlg_write.show()
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...