满足条件时,在Android中显示按钮

时间:2015-03-21 01:04:10

标签: android button

正如标题所说,我想知道在我的代码中满足某些条件时生成按钮的最佳方法。在这种情况下,我想点击特定的图像查看" s02"在我的活动中出现一个按钮。

我知道您可以使用以下代码显示AlertDialog:

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 for AlertDialog,但我无法在Button上使用Builder。

另外,我应该在单独的代码部分创建按钮,然后只是在条件设置时显示,或者我应该在创建按钮的代码中放置按钮的功能吗?

1 个答案:

答案 0 :(得分:2)

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