2012-05-09 118 views
1

當我的警報彈出時,我希望它在關閉時再次觸摸,而無需添加任何按鈕。如何在沒有按鈕的情況下關閉AlertDialog.Builder

等地圖圖釘觸摸

@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

有什麼特別的理由不使用PopupWindow?它可以讓你很容易地收到「內部」和「外部」觸摸事件的通知。

+0

那麼當我嘗試PopupWindow它不會在我的tabbar視圖內我的mapview內工作。本身的mapview視圖是沒有問題的,但是當將所有東西包裝在tabbar調用中時,它不起作用。 – Rick

+1

你將哪個錨點傳遞給'PopupWindow'?確保它位於前景可見/聚焦的「視圖」中。你的'PopupWindow'沒有理由不能在你的情況下工作,並且它對於你的目的來說是更乾淨的解決方案。 – avimak

+0

我爲一個彈出窗口添加了一些代碼(commonsware)。在其自己的應用程序按預期工作,但在我的窗口不會顯示 – Rick

相關問題