在对话框中添加肯定按钮 [英] Add positive button to Dialog

查看:103
本文介绍了在对话框中添加肯定按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的自定义对话框,我不想在不修改XML文件的情况下添加肯定按钮,就像您使用AlertDialog一样,但是我不知道是否可能.这是代码:

I have a very simple custom dialog and I wan't to add a positive button without having to modify the XML file, just like you would do it with an AlertDialog but I don't know if it's possible. This is the code:

final Dialog dialog = new Dialog(MyActivity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Settings");
dialog.show();

推荐答案

您应该使用构建器.

LayoutInflater inflater = LayoutInflater.from(this);
View dialog_layout = inflater.inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.dialog_root_layout));
AlertDialog.Builder db = new AlertDialog.Builder(MyActivity.this);
db.setView(dialog_layout);
db.setTitle("settings");
db.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    }
});
AlertDialog dialog = db.show();

这篇关于在对话框中添加肯定按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆