开发者

Unable to find a button in AlertDialog until I call Show.. why? Shouldnt it be available after CREATE?

开发者 https://www.devze.com 2023-03-13 10:09 出处:网络
I have an alert dialog, that I have 2 text fields and then a Positive and Negative button. I was trying to set the nextFocusDown on the final text field to ALWAYS be the positive button.

I have an alert dialog, that I have 2 text fields and then a Positive and Negative button.

I was trying to set the nextFocusDown on the final text field to ALWAYS be the positive button.

The positive and negative buttons are layed out beside each other below the second text field and if the cursor for user input was over the left button the nextfocus would move to the left button, if it was over the right button it would move to the right button.

So I use my alert builder to CREATE my alert Dialog and then attempted to call

myAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE); and then set the ExitText fields nextFocusDownId to that button's ID.

and do this before I show the Alert to the user, so I don't have the slightest chance of any racing conditions.

Alas, the ad.getButton returns NULL every single time. When I move these lines of code to after the ad.show() call, everything works perfectly.

So I guess the question I have is, can someone explain this to me. I would think once I have SET THE POSITIVE BUTTON and created the AlertDialog that the view should exist but not be visible... obviously this is not the case, so does nothing that is a DisplayWidget get create until the view is actually shown as a general ru开发者_开发百科le of thumb? When views are inflated I am able to find objects within them before I show them. So what is the general rule of thumb regarding this?


I think your final statements/questions are on the right track. The call to show() on the dialog inflates and shows the view. So in sort of makes sense that you cannot interact with the view in code until the dialog is shown. I don't see there being much of a race condition if your code to manipulate the dialog comes directly after calling show().

Specifically, here is what the javadoc says:

show() - Start the dialog and display it on screen.

So the dialog is not "started" until show() is called.


Here is example of the code populating the dialog. May be you should use similar approach and then set up your buttons:

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.promptdialog, null);
//get a builder and set the view
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Prompt");
builder.setView(view);


These are limitations of using the AlertDialog.Builder. As suggested, your views are not inflated yet. This doesn't necessarily mean that there's no way to access these views until the dialog is shown. You can inflate the views yourself and modify at will. See the docs on custom dialogs. You can inflate your own views and then use setView to set the View for the Dialog. Unfortunately this does negate a lot of the simplicity that the Builder provides.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号