自定义AlertDialog框未显示

时间:2016-09-03 04:27:30

标签: android alertdialog android-alertdialog android-dialog

在我的应用程序中,当我尝试显示自定义AlertDialog框时,它在Android手机中工作正常。现在,当我在我的android选项卡上安装应用程序时,一切只能完成自定义AlertDialog框的问题。它没有显示。所以我想,我应该检查正常的对话框,它工作正常。以下是普通对话框和警告对话框的代码。

普通对话:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");

// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.delete);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                // Write your code here to invoke YES event
                Toast.makeText(getApplicationContext(),
                        "You clicked on YES", Toast.LENGTH_SHORT)
                        .show();
            }
        });

// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to invoke NO event
                Toast.makeText(getApplicationContext(),
                        "You clicked on NO", Toast.LENGTH_SHORT)
                        .show();
                dialog.cancel();
            }
        });

// Showing Alert Message
alertDialog.show();

自定义布局对话框

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());

// AlertDialogBuilder.setMessage(diadis);
LayoutInflater inflater = (LayoutInflater)getApplicationContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.update_status, null);
alertDialogBuilder.setView(view);

final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
alertDialog.show();
alertDialog.findViewById(R.id.positive_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});

// Close
alertDialog.findViewById(R.id.close_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});

2 个答案:

答案 0 :(得分:0)

查看此代码段

private void openQuitDialog() {
    AlertDialog.Builder quitDialog
            = new AlertDialog.Builder(MainActivity.this);
    quitDialog.setTitle(getResources().getString(R.string.are_you_sure_to_exit));
    quitDialog.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    quitDialog.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });

    quitDialog.show();
}

并在您想要的位置调用openQuitDialog()。

答案 1 :(得分:0)

为此您需要添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

还要感谢CommonsWare's blog post,与Marshmallow的运行时间

假设您的代码位于ActivityFragment,请检查重叠权限并在必要时提出请求:

public static int OVERLAY_PERMISSION_REQ_CODE = 1234;

在alertdialog

之前调用此方法
 checkDrawOverlayPermission();

将方法复制到您的班级

public void checkDrawOverlayPermission() {
    if (!Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
    }
}

然后,重新检查权限以获得更好的用户体验:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
        if (!Settings.canDrawOverlays(this)) {
            // SYSTEM_ALERT_WINDOW permission not granted...
        }
    }
}

当您将此代码添加到项目中时,在gif等marashmallow中绘制项目时会有一些漏洞 enter image description here