2014-02-22 27 views
7

我創建了一個擴展popupwindow的類。它的構造看起來像下面Android Click Click PopupWindow

super(builder.context.get()); 

this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT, 
     ViewGroup.LayoutParams.MATCH_PARENT); 
setFocusable(true); 
setBackgroundDrawable(new BitmapDrawable()); 
setTouchInterceptor(onTouchListener); 
FrameLayout frameLayout = new FrameLayout(builder.context.get()); 
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
     Context.LAYOUT_INFLATER_SERVICE); 
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false); 
frameLayout.addView(builder.backgroundView); 
frameLayout.addView(overlayBase); 
setContentView(frameLayout); 

的onTouchListener如下所示:

private OnTouchListener onTouchListener = new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 
     Log.d(TAG, "Received"); 

     if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) { 
      Log.d(TAG, "bubbled through"); 
      return false; 
     } 
     return true; 
    } 
}; 

實際顯示popupwindow,我叫SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

可視表示,這裏是有問題的產品

blah

touchListener正在響應,但輸入不會被髮送到基礎活動或彈出窗口? (該按鈕沒有響應,就好像我要刪除ontouchlistener一樣,按鈕工作正常)。

更新 設置觸摸偵聽到的FrameLayout(在這種情況下,即popupwindow的內容視圖),而不是popupwindow本身允許我點擊在彈出定義的按鈕。還在尋找方法來觸摸事件委託給底層活動/圖

SOLUTION

註冊爲Popupwindow觸摸攔截。如果您希望該活動處理該活動,則假設您有該活動的參考,請撥打someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);,如果您希望該彈出窗口處理該觸摸,請撥打someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)至彈出窗口的內容視圖,如在彈出窗口setContentView(someView)方法中設置的。

我的方法具體看起來像下面

private OnTouchListener onTouchListener = new OnTouchListener() { 
@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
       radius, 2)) { 
      activity.get().dispatchTouchEvent(event); 
      return false; 
     } 
     frameLayout.dispatchTouchEvent(event); 
     return true; 
    } 
}; 
+0

你能否澄清是否「鼓入」實際上是被你期望落空彈出的點擊記錄? – NameSpace

+0

是的,對於混淆抱歉。 ontouchlistener被調用,並且根據我觸摸的位置(即,當我在圓圈區域中觸摸時「冒泡」),Log輸出相應地跟隨 – rperryng

+0

然後不確定。如果您需要黑客解決方案,可以檢查觸摸是否在按鈕的xy座標中並執行performClick()。或者,也可以將觸摸明確地傳遞給包含按鈕的Activity/View的onTouch。希望有人能給你一個更好的答案。 – NameSpace

回答

3

SOLUTION

註冊爲Popupwindow觸摸攔截。如果您希望該活動處理該活動,則假設您有該活動的參考,請撥打someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);,如果您希望該彈出窗口處理該觸摸,請撥打someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)至彈出窗口的內容視圖,如在彈出窗口setContentView(someView)方法中設置的。

我的方法具體看起來像下面

private OnTouchListener onTouchListener = new OnTouchListener() { 
@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
       radius, 2)) { 
      activity.get().dispatchTouchEvent(event); 
      return false; 
     } 
     frameLayout.dispatchTouchEvent(event); 
     return true; 
    } 
}; 
+0

你好? 'theMotionEventPassedToTouchInterceptor'你可以告訴更多細節如何做到這一點? –

+0

看看如何傳遞給'onTouchListener'的參數是'MotionEvent'類型?如果你有一個對基礎活動的引用,那麼通過調用yourActivity.dispatchTouchEvent(motionEvent)來傳遞它觸摸事件(然後rrturn false!) – rperryng

+0

這裏的這個權利,在被卡住3天后給了我一個解決方案。你是男人! –

0
public class TestPopupActivity extends Activity { 

//The "x" and "y" position of the "Show Button" on screen. 
Point p; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button btn_show = (Button) findViewById(R.id.show_popup); 
    btn_show.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 

     //Open popup window 
     if (p != null) 
     showPopup(TestPopupActivity.this, p); 
    } 
    }); 
} 

// Get the x and y position after the button is draw on screen 
// (It's important to note that we can't get the position in the onCreate(), 
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0)) 
@Override 
public void onWindowFocusChanged(boolean hasFocus) { 

    int[] location = new int[2]; 
    Button button = (Button) findViewById(R.id.show_popup); 

    // Get the x, y location and store it in the location[] array 
    // location[0] = x, location[1] = y. 
    button.getLocationOnScreen(location); 

    //Initialize the Point with x, and y positions 
    p = new Point(); 
    p.x = location[0]; 
    p.y = location[1]; 
} 

// The method that displays the popup. 
private void showPopup(final Activity context, Point p) { 
    int popupWidth = 200; 
    int popupHeight = 150; 

    // Inflate the popup_layout.xml 
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup); 
    LayoutInflater layoutInflater = (LayoutInflater) context 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup); 

    // Creating the PopupWindow 
    final PopupWindow popup = new PopupWindow(context); 
    popup.setContentView(layout); 
    popup.setWidth(popupWidth); 
    popup.setHeight(popupHeight); 
    popup.setFocusable(true); 

    // Some offset to align the popup a bit to the right, and a bit down, relative to button's position. 
    int OFFSET_X = 30; 
    int OFFSET_Y = 30; 

    // Clear the default translucent background 
    popup.setBackgroundDrawable(new BitmapDrawable()); 

    // Displaying the popup at the specified location, + offsets. 
    popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y); 

    // Getting a reference to Close button, and close the popup when clicked. 
    Button close = (Button) layout.findViewById(R.id.close); 
    close.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     popup.dismiss(); 
    } 
    }); 
} 
} 

試試這個代碼可能是它幫助你。 ..。 。 或者可能是這個鏈接將幫助ü更多.. http://mrbool.com/how-to-implement-popup-window-in-android/28285

+0

恩,謝謝?我沒有問題膨脹的窗口。問題是我無法將觸摸事件轉發到彈出窗口錨定的視圖。據我所見,這個實現不在你提供的代碼片段中 – rperryng

+0

Rperryng ...是否有必要粗魯? 「恩,謝謝?」因爲Hitesh在解釋你的問題時試圖提供幫助是不恰當的。 –

+0

你是對的。這是不恰當的。對不起Hitesh! – rperryng