如何根据给定条件在 activity 的 onCreate 方法中显示按钮?

How can I display a button in the onCreate method of an activity based on a given condition?

我想在 activity 中显示一个按钮,但如果用户满足特定条件,按钮的文本和背景就会不同。我如何在 OnCreate 方法中初始化同一个按钮,根据条件有两种不同的设计?

1。您可以在 运行 时间内更改此属性。

2。您可以创建两个可绘制文件以在按钮背景中设置,但您仍然需要在 运行 时间内更改文本。

3。您可以使用布局数据在 xml 文件中使用数据绑定,例如: Google Link

这里也许有帮助

 val btnGet = findViewById<Button>(R.id.btnGet)

    if (id == 1){   //Your condition 

        btnGet.text = id.toString()
        btnGet.visibility = View.VISIBLE    //Make Sure in xml it is gone 
        btnGet.setTextColor(Color.parseColor("#bdbdbd"))
        btnGet.setBackgroundColor(Color.parseColor("#ffffff"))
    }else{
        btnGet.text = id.toString()
         btnGet.visibility = View.VISIBLE
        btnGet.setTextColor(Color.parseColor("#FFBB86FC"))
        btnGet.setBackgroundColor(Color.parseColor("#FF03DAC5"))

    }

如果你想要完整的代码,我会与你分享 快乐编码