繁体   English   中英

自定义AlertDialog框未显示

[英]Custom AlertDialog box not showing

在我的应用程序中,当我尝试显示自定义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();
    }
});

看看这段代码

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()。

为此,您需要添加权限

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

还要感谢CommonsWare的博客文章 ,以及与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);
    }
}

然后,重新检查权限以获得更好的UX:

@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...
        }
    }
}

当您将此代码添加到项目中时,您可以在marashmallow(如gif)中绘制项目 在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM