Java – the button in the alertdialog does not appear

I have an alertdialog box where the neutral button should cause me to disappear. The problem is that when the alertdialog box appears, the text set looks good, but the neutral button is not displayed at all. I try to use it as a positive and negative button, but there is no difference. This is the code:

Button infoG1;

    infoG1 = (Button) findViewById(R.id.iG1);
    infoG1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder info = new AlertDialog.Builder(game1.this);
            info.setMessage("Text here");
                    info.show();
                    info.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });


        }
    });

I had expected a button with the text "OK" to appear at the bottom of the alert dialogue box, which would cause the box to disappear after the user pressed it

resolvent:

You should not call setNeutralButton (...) after the show () method. You do this, which is a problem.

Not this:

dialog.show();
dialog.setNeutralButton(...);

Do this:

dialog.setNeutralButton(...);
dialog.show();

Please note that you should put everything above dialog. Show(). Otherwise, you will encounter problems

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>