如何在没有按钮的情况下触发AlertDialog.Builder

时间:2012-05-09 19:45:38

标签: android

当我的警报弹出时,我想让它在再次触摸时解除,不添加任何按钮。

所以在地图上触针

@Override
    protected boolean onTap(int i) {
        OverlayItem item = items.get(i);    
        dialog.setTitle(item.getSnippet());
        dialog.setMessage(item.getTitle());
        dialog.show();
        return (true);
    }

那么如何通过触摸警报或背景中的任何位置来消除它。

编辑:添加代码

 private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private Drawable heart=null;
private List<CustomItem> items=new ArrayList<CustomItem>();
private PopupPanel panel=new PopupPanel(R.layout.popup);

public SitesOverlay() {
  super(null);

  heart=getMarker(R.drawable.heart_full);

  JSONObject json;
  JSONObject json2;
  String resultURL = queryRESTurl("http://www.kickintheapp.com/api/maps393/json.json");
  try {
      json = new JSONObject(resultURL);

  JSONArray nameArray = json.names();

  JSONArray valArray;

      valArray = json.toJSONArray(nameArray);


  for (int i = 0; i < valArray.length(); i++) 
  {

      Log.e(nameArray.getString(i), valArray.getString(i));
      json2 = new JSONObject(valArray.getString(i));
      JSONArray nameArray2 = json2.names();

      JSONArray valArray2 = json2.toJSONArray(nameArray2);
      for (int a = 0; a < valArray2.length(); a++) 
      {

          // add to maps here
          items.add(new CustomItem(getPoint(valArray2.getDouble(3),valArray2.getDouble(2)), valArray2.getString(1), valArray2.getString(0),getMarker(R.drawable.pin_yellow), heart));
          Log.e(nameArray2.getString(a), valArray2.getString(a));
      }
   }
  } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }

  populate();
}

@Override
protected CustomItem createItem(int i) {
  return(items.get(i));
}

@Override
public void draw(Canvas canvas, MapView mapView,
                  boolean shadow) {
  super.draw(canvas, mapView, shadow);

}

@Override
protected boolean onTap(int i) {
    OverlayItem item=getItem(i);
    GeoPoint geo=item.getPoint();
    Point pt=map.getProjection().toPixels(geo, null);

    View view=panel.getView();

    ((TextView)view.findViewById(R.id.latitude))
      .setText(item.getSnippet());
    ((TextView)view.findViewById(R.id.longitude))
      .setText(item.getTitle());

    panel.show(pt.y*2>map.getHeight());

    return(true);
}

class PopupPanel {
   View popup;
   boolean isVisible=false;

 PopupPanel(int layout) {
   ViewGroup parent=(ViewGroup)map.getParent();

   popup=getLayoutInflater().inflate(layout, parent, false);

   popup.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      hide();
    }
  });
}

View getView() {
  return(popup);
}

void show(boolean alignTop) {
  RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT
  );

  if (alignTop) {
    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lp.setMargins(0, 20, 0, 0);
  }
  else {
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lp.setMargins(0, 0, 0, 60);
  }

  hide();

  ((ViewGroup)map.getParent()).addView(popup, lp);
  isVisible=true;
}

void hide() {
  if (isVisible) {
    isVisible=false;
    ((ViewGroup)popup.getParent()).removeView(popup);
  }
 }
}

1 个答案:

答案 0 :(得分:1)

不使用PopupWindow的任何特殊原因?它允许您非常轻松地收到“内部”和“外部”触摸事件的通知。