50

我打算用 layout_weight=1 创建 3 个按钮,对自定义对话框不感兴趣。所以我写了下面的代码。它不起作用。总是是按钮给我 null。这段代码有什么问题?

  AlertDialog dialog= new AlertDialog.Builder(this).create();
            dialog.setIcon(R.drawable.alert_icon);
            dialog.setTitle("title");
            dialog.setMessage("Message");
            dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                                                }
            });
            Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            Log.w("Button",""+yesButton);//here getting null
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);
            yesButton.setLayoutParams(layoutParams);
            dialog.show();

问候,Android 开发者。

4

4 回答 4

89

看看这里的答案:http ://code.google.com/p/android/issues/detail?id=6360

正如评论 #4 中所说,您必须先调用show()对话框,然后才能访问按钮,它们事先不可用。有关如何在按钮准备好后立即修改按钮的自动解决方案,请参阅Mickeys 回答

于 2011-01-05T12:50:25.813 回答
72

这对我有用:

AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setMessage(message)
                .setCancelable(true)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            //do smthng
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //do snthn
                    }
                }).create();

        alertDialog.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {                    //
                Button positiveButton = ((AlertDialog) dialog)
                        .getButton(AlertDialog.BUTTON_POSITIVE);
                positiveButton.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.btn_default_holo_dark));

                Button negativeButton = ((AlertDialog) dialog)
                        .getButton(AlertDialog.BUTTON_NEGATIVE);
                positiveButton.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.btn_default_holo_dark));
            }
        });

        alertDialog.show(); 

仅按此顺序,alertDialog.setOnShowListener后调用create()

于 2013-01-30T10:16:04.303 回答
6

谢谢维厄。但是对于新开发人员的理解目的,我正在重写下面的代码

AlertDialog dialog= new AlertDialog.Builder(this).create();             
dialog.setIcon(R.drawable.alert_icon);             
dialog.setTitle("title");            
dialog.setMessage("Message");             
dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {                 
    @Override                 
    public void onClick(DialogInterface arg0, int arg1) {                                                
    }             
}); 
dialog.show(); 
Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);             
Log.w("Button",""+yesButton); //here getting null             
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);             
yesButton.setLayoutParams(layoutParams);        
于 2011-01-05T13:50:47.420 回答
0

以下是有用的 Kotlin 扩展功能,可通过自动解决方案实现您想要的功能:

import android.content.DialogInterface.BUTTON_NEGATIVE
import android.content.DialogInterface.BUTTON_POSITIVE
import androidx.appcompat.app.AlertDialog

fun AlertDialog.onPositiveButtonClick(action: () -> Unit) {
    onButtonClick(BUTTON_POSITIVE, action)
}

fun AlertDialog.onNegativeButtonClick(action: () -> Unit) {
    onButtonClick(BUTTON_NEGATIVE, action)
}

fun AlertDialog.onButtonClick(whichButton: Int, action: () -> Unit) {
    fun onButtonClick() = getButton(whichButton).setOnClickListener { action() }

    if (isShowing) {
        onButtonClick()
    } else {
        setOnShowListener {
            onButtonClick()
        }
    }
}

如何使用 :

alertDialog.onPositiveButtonClick { /* DO WHAT YOU WANT HERE */ }
于 2021-09-10T13:48:33.133 回答