如何在 kotlin 中将 html 与 if else 按钮链接

问题描述 投票:0回答:1

我想将我的应用程序中的按钮与网站链接,因为当用户单击此按钮时,网站会在浏览器中打开,但当我单击按钮时,我不断看到 ViewPostIme 指针 0 和 ViewPostIme 指针 1。这是我的代码:

val viewButton = findViewById<Button>(R.id.view_button)
        viewButton.setOnClickListener {
            val macAddress = scanResultBinding.deviceAddress.toString(){
            val url = if (macAddress.isNotEmpty() && macAddress == "//macAddress") {
                "MyWebsiteStore"
            } else {
                "MyWebsiteHome/"
            }
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse(url)
            startActivity(intent)
        }
    }
kotlin if-statement button
1个回答
0
投票

您的代码中似乎存在语法错误。在

}
之后有一个额外的大括号
scanResultBinding.deviceAddress.toString()
。尝试删除它,看看是否能解决问题。您的代码应如下所示:

val viewButton = findViewById<Button>(R.id.view_button)
viewButton.setOnClickListener {
    val macAddress = scanResultBinding.deviceAddress.toString()
    val url = if (macAddress.isNotEmpty() && macAddress == "//macAddress") {
        "MyWebsiteStore"
    } else {
        "MyWebsiteHome/"
    }
    val intent = Intent(Intent.ACTION_VIEW)
    intent.data = Uri.parse(url)
    startActivity(intent)
}

这应该可以解决“ViewPostIme 指针”消息的问题。

© www.soinside.com 2019 - 2024. All rights reserved.