在Dialog中添加正按钮

Joe*_*yCK 9 android android-layout

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

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

amp*_*amp 12

您应该使用构建器.

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();
Run Code Online (Sandbox Code Playgroud)