简体   繁体   English

Android中的自定义AlertDialog.Builder类不会显示

[英]Custom AlertDialog.Builder class in android won't show

I'm currently writing a program that will use the Geocoder to search for possible GeoPoints of a city search. 我目前正在编写一个程序,该程序将使用Geocoder搜索城市搜索中可能的GeoPoints。 I then take the geopoints and add it to a map as overlays, the user can then click the overlay, and an alert dialog will pop up to ask if he/she is sure that this is the right one. 然后,我将这些地理点作为叠加层添加到地图上,然后用户可以单击该叠加层,然后会弹出一个警告对话框,询问他/她是否确定这是正确的。

I couldn't figure out a way to get the alert dialog to work like swing where after the user clicks yes or no, I can retrieve the answer. 我想不出一种方法来使警报对话框像挥杆一样工作,在用户单击“是”或“否”之后,我可以检索答案。 So I extended the AlertDialog.Builder class like so, which also happens to be a Dialog.OnClicklistener 所以我像这样扩展了AlertDialog.Builder类,它也恰好是Dialog.OnClicklistener

public class MyAlertDialog extends AlertDialog.Builder implements DialogInterface.OnClickListener{ 
final static int positiveMessage = 1;
final static int negativeMessage = 0; 
final static int neutralMessage = -1;

private int myMessage; 

public MyAlertDialog(Context activity) {
    super(activity);
}

@Override
public void onClick(DialogInterface dialog, int which) {
    if(which == dialog.BUTTON_POSITIVE){
        myMessage = positiveMessage;
    }
    else if(which == dialog.BUTTON_NEGATIVE){
        myMessage = negativeMessage;
    }
    else{
        myMessage = neutralMessage;
    }
}

public int getMessage() {
    return myMessage;
}

and I implement it like so 我像这样实现

    protected boolean onTap(int index) {

    OverlayItem item = overlays.get(index);
      MyAlertDialog dialog = new MyAlertDialog(ctx);
      dialog.setTitle(item.getTitle());
      dialog.setMessage("Is this the " + item.getTitle()
              + " you're looking for?");
      dialog.setPositiveButton("Yes",null);
      dialog.setNegativeButton("Cancel", null);
      dialog.show();

      if(dialog.getMessage()== MyAlertDialog.positiveMessage){
               //do some stuff

But for some reason the dialog wont show until after the method has returned, so it never does the stuff. 但是由于某种原因,该对话框直到方法返回后才显示,因此它从不执行任何操作。 Anyone have any ideas? 有人有想法么? Oh and ctx is a reference to my mapActivity 哦,ctx是对我的mapActivity的引用

This is because the dialog.show(); 这是因为dialog.show(); method does not wait for the user to interact with the Dialog before returning. 方法在返回之前不会等待用户与Dialog交互。 It does exactly what the name would suggest, and nothing more; 它的名称完全符合其含义,仅此而已。 it shows the Dialog, and then returns. 显示对话框,然后返回。 So, that means that your myMessage field will always be null and this condition will never be true: 因此,这意味着您的myMessage字段将始终为null,并且此条件永远不会为true:

      if(dialog.getMessage()== MyAlertDialog.positiveMessage){

What you should do instead is pass in OnClickListener for both your positive and negative button, and do whatever you need to in the respective OnClickListener. 相反,您应该做的是为肯定按钮和否定按钮传递OnClickListener,并在相应的OnClickListener中进行所需的操作。 You won't even need to make a subclass of AlertDialog.Builder, because there won't be any benefit to doing that. 您甚至不需要制作AlertDialog.Builder的子类,因为这样做没有任何好处。 Here's how that looks: 看起来是这样的:

dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which){
        // Do some positive stuff here!
    }
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which){
        // Do some negative stuff here!
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM