0

我有进度对话框:

mProgressDialog = new ProgressDialog(WebViewActivity.this);

有一些像这样的按钮:

mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
            @Override
             public void onClick(DialogInterface dialog, int which) {
                //some code
            }
         });

我正在尝试通过(并获取 NullPointerException)设置它们的参数:

Button cancel = mProgressDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
int width1 = (int) TypedValue.applyDimension
                (TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics());
         **NullPointerException here:** cancel.setLayoutParams(new LinearLayout.LayoutParams
                 (width1, LayoutParams.MATCH_PARENT, 1f));

那么,如何在此处设置按钮的布局参数(不使用自定义进度对话框)?有些东西告诉我我根本无法设置它们,而不是使用自定义进度对话框......

PS:对不起,如果我在这里遗漏了一些非常简单的东西

4

2 回答 2

1

这些按钮在 Dialog.onCreate() 之后安装到对话框中。因此,您可以覆盖此方法并在此处添加代码,或者您必须在调用 Dialog.show() 后获取按钮。

这是我的示例,我更改了第一个按钮的重量。

    ProgressDialog dialog = new ProgressDialog(this);
    dialog.setButton(AlertDialog.BUTTON_POSITIVE, "confirm",
            (DialogInterface.OnClickListener) null);
    dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "cancel",
            (DialogInterface.OnClickListener) null);
    dialog.show();
    Button btConfirm = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btConfirm
            .getLayoutParams();
    params.weight = 3;
于 2012-12-11T16:36:32.237 回答
1

这里是带有构建器的 AlertDialog 的代码

            AlertDialog.Builder builder = new AlertDialog.Builder(Setup.this);
            builder.setMessage("Message", 1))
            .setPositiveButton(getString(R.string.browseonlinesupport), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
            })
            .setNeutralButton(getString(R.string.submit_help_request), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            })
            .setNegativeButton(getString(R.string.cancel), null) // to make the button even widths
            //.show()
            ;
            //                }
            AlertDialog helpDialog = builder.create();
            helpDialog.show();  // call show() must from dialog not builder, otherwise button not created and getButton is null
            LinearLayout.LayoutParams btParams;
            Button btPositive = helpDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            btParams = (LinearLayout.LayoutParams) btPositive.getLayoutParams();
            btParams.weight = 1;
            btParams.width = btParams.MATCH_PARENT;

            Button btNegative = helpDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            btParams = (LinearLayout.LayoutParams) btNegative.getLayoutParams();
            btParams.weight = 1;
            btParams.width = btParams.MATCH_PARENT;

            Button btNeutral = helpDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            btParams = (LinearLayout.LayoutParams) btNeutral.getLayoutParams();
            btParams.weight = 1;
            btParams.width = btParams.MATCH_PARENT;

*,调用 show() 必须从对话框而不是构建器,否则按钮未创建且 getButton 为空

于 2013-09-18T21:56:09.420 回答