【问题标题】:AlertDialog not displaying Neutral Button IconAlertDialog 不显示中性按钮图标
【发布时间】:2020-08-08 18:13:47
【问题描述】:

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

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

示例输出如下:Click here to see sample output with icons attached.

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

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

此处显示带有文本而不是图标的输出。 Click here to see sample output with text.

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

【问题讨论】:

    标签: android android-studio android-activity android-alertdialog


    【解决方案1】:

    请试试这个代码,它对我来说很好用

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

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

    【讨论】:

    • 感谢@AndroidGeek,这段代码运行良好。只是在调整大小时遇到​​了一些问题,就是这样。也解决了它们。再次感谢,先生!
    • 花了我几个小时才知道我需要import androidx.appcompat.app.AlertDialog 而不是import android.app.AlertDialog 才能看到setNeutralButtonIcon 方法。
    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多