当满足条件时,使按钮出现在 Android 中

Making a button appear in Android when a condition is fulfilled

正如标题所说,我想知道在我的代码中满足某些条件时生成按钮的最佳方法。在这种情况下,我想单击特定的 imageView "s02" 以使按钮出现在我的 activity.

我知道您可以使用如下代码使 AlertDialogs 出现:

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Title");
            alertDialog.setMessage("Message");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // code code code code code  }
                }); 

我尝试在第一行用 Button 替换 AlertDialog,但我无法在 Button 上使用 Builder。

此外,我应该在单独的代码部分创建按钮,然后在设置条件时简单地使其显示,还是应该将按钮的功能放在创建按钮的代码中?

将您的按钮放在 xml 布局中并使其在需要时显示是最简单的方法。如果这还不够好,你可以通过new Button(Context)创建一个Button,设置你需要的所有参数,然后将它添加到父布局。