AlertDialog不显示Neutral按钮图标。

问题描述 投票:0回答:1

我是一个没有经验的android开发新手.当我的方法被创建时,我正在显示一个提示对话框来选择被调用的活动的选项.但是,它没有显示中性按钮图标,但它调用相关的动作.它在点击它时显示图像。请参考下面给出的代码和图片链接,代码是这样的。

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

示例输出是这样的: 点击这里查看带有图标的示例输出。

然而,在删除图标并添加标题时,它显示的是文本。另一个带文字的代码是这样的。

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

这里显示的是带文字而不是图标的输出。点击这里查看带文字的输出示例。

我应该怎么做?有什么其他建议可以改善我的UI吗?请帮助我。

android android-studio android-activity android-alertdialog
1个回答
0
投票

请尝试这个代码,它的工作对我来说很好

由于没有netural按钮文字图标不可见,所以我在netural按钮文字上加了一个空格,并在显示对话框后设置了按钮图标代码。

   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);

Screen Shot

© www.soinside.com 2019 - 2024. All rights reserved.