繁体   English   中英

AlertDialog 不显示中性按钮图标

[英]AlertDialog not displaying Neutral Button Icon

我是 android 开发的新手。 创建我的方法后,我将显示一个警报对话框,显示被调用活动的 select 选项。 但是,它不显示中性按钮图标,而是调用相关操作。 点击它会显示图像。 请参考下面给出的代码和图片链接。 代码是这样的:

 initDialogBuilder.setCancelable(false)
                    .setTitle("Select your counter")
                    .setPositiveButtonIcon(getDrawable(R.drawable.o))
                    .setPositiveButton("",listener)
                    .setNegativeButtonIcon(getDrawable(R.drawable.x))
                    .setNegativeButton("",listener)
                    .setNeutralButtonIcon(getDrawable(R.drawable.sq))
                    .setNeutralButton("",listener)
                    .setMessage("Please select your counter.");
            AlertDialog initDialog = initDialogBuilder.create();
            initDialog.show();

样品 output 是这样的:单击此处查看带有图标的样品 output。

但是,在删除图标并添加标题时,它会显示文本。 另一个带有文本的代码是这样的:

initDialogBuilder.setCancelable(false)
                    .setTitle("Select your counter")
                    .setPositiveButton("X",listener)
                    .setNegativeButton("O",listener)
                    .setNeutralButton("SQ",listener)
                    .setMessage("Please select your counter.");
            AlertDialog initDialog = initDialogBuilder.create();
            initDialog.show();

此处显示的 output 带有文本而不是图标。 单击此处查看带有文本的示例 output。

我应该怎么办? 还有其他改进我的 UI 的建议吗? 请帮忙。

请尝试此代码,它对我来说工作正常

没有不可见的神经按钮文本图标,所以我在神经按钮文本中添加了一个空格并在显示对话框后设置按钮图标代码。检查下面的代码和屏幕截图

   AlertDialog.Builder builder;
    builder = new AlertDialog.Builder(this);
    //Uncomment the below code to Set the message and title from the strings.xml file
    builder.setMessage("Custom dialog with neutral button") .setTitle("Just R&D");

    //Setting message manually and performing action on button click
    builder.setMessage("Do you want to close this application ?")
            .setCancelable(false)
            .setPositiveButton("", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                    Toast.makeText(getApplicationContext(),"you choose yes action for alertbox",
                            Toast.LENGTH_SHORT).show();
                }
            }).setPositiveButtonIcon(getDrawable(R.drawable.ic_android_black_24dp))
            .setNegativeButton("", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //  Action for 'NO' Button
                    dialog.cancel();
                    Toast.makeText(getApplicationContext(),"you choose no action for alertbox",
                            Toast.LENGTH_SHORT).show();
                }
            }).setNegativeButtonIcon(getDrawable(R.drawable.ic_android_black_24dp)).setNeutralButton(" ", new DialogInterface.OnClickListener() { //need to add neutral button text
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    })/*.setNeutralButtonIcon(getDrawable(R.drawable.ic_android_black_24dp))*/;
    //Creating dialog box
    AlertDialog alert = builder.create();
    //Setting the title manually
    alert.setTitle("AlertDialogExample");
    alert.show();

    Button button = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
    Drawable drawable = this.getResources().getDrawable(
            android.R.drawable.ic_media_play);

    // set the bounds to place the drawable a bit right
    drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5),
            0, (int) (drawable.getIntrinsicWidth() * 1.5),
            drawable.getIntrinsicHeight());
    button.setCompoundDrawables(drawable, null, null, null);

截屏

暂无
暂无

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

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