Java android中的自定义AlertDialog.Builder类赢得';不显示
我目前正在编写一个程序,将使用地理编码器搜索城市搜索的可能地理点。然后,我获取地质点并将其作为覆盖添加到地图中,用户可以单击覆盖,然后会弹出一个警报对话框,询问他/她是否确定这是正确的覆盖 我想不出一种方法来让警报对话框像swing一样工作,在用户单击yes或no之后,我可以检索答案。所以我像这样扩展了AlertDialog.Builder类,它也恰好是Dialog.OnClicklistenerJava android中的自定义AlertDialog.Builder类赢得';不显示,java,android,sqlite,google-maps,Java,Android,Sqlite,Google Maps,我目前正在编写一个程序,将使用地理编码器搜索城市搜索的可能地理点。然后,我获取地质点并将其作为覆盖添加到地图中,用户可以单击覆盖,然后会弹出一个警报对话框,询问他/她是否确定这是正确的覆盖 我想不出一种方法来让警报对话框像swing一样工作,在用户单击yes或no之后,我可以检索答案。所以我像这样扩展了AlertDialog.Builder类,它也恰好是Dialog.OnClicklistener public class MyAlertDialog extends AlertDialog.Bu
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;
}
我就是这样实现的
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
但由于某些原因,对话框在方法返回后才会显示,因此它永远不会执行这些操作。有人有什么想法吗?Oh和ctx是对我的mapActivity的引用,这是因为
对话框.show()方法在返回之前不会等待用户与对话框交互。它完全按照名字所暗示的去做,仅此而已;它显示对话框,然后返回。因此,这意味着您的myMessage字段将始终为空,并且此条件永远不会为真:
if(dialog.getMessage()== MyAlertDialog.positiveMessage){
您应该做的是将OnClickListener传递给您的肯定和否定按钮,并在相应的OnClickListener中执行您需要的任何操作。您甚至不需要创建AlertDialog.Builder的子类,因为这样做没有任何好处。这看起来是这样的:
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!
}
});
我想,由于它是在GUI线程上运行的,所以警报对话框会在继续之前等待输入,但我想不会。我想那就行了。谢谢,没问题。show方法确实在GUI线程上运行,但其实现方式并不等待用户输入。它向屏幕添加一个视图,然后立即返回,因此GUI线程可以继续执行。以下是Android源代码中实际show()方法的链接: