2011-02-03 66 views
5

我目前正試圖獲得一個自定義WebView,當它按下更長時間時會顯示ContextMenu。當一個鏈接longPressed默認的WebView類只顯示一個文本菜單,我寫我自己的類重寫此行爲:Android:從onLongPress打開自定義WebView中的ContextMenu

public class MyWebView extends WebView { 
    Context context; 
    GestureDetector gd; 

    public MyWebView(Context context, AttributeSet attributes) { 
     super(context, attributes); 
     this.context = context; 
     gd = new GestureDetector(context, sogl); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return gd.onTouchEvent(event); 
    } 

    GestureDetector.SimpleOnGestureListener sogl = 
       new GestureDetector.SimpleOnGestureListener() { 

     public boolean onDown(MotionEvent event) { 
      return true; 
     } 

     public void onLongPress(MotionEvent event) { 
      // The ContextMenu should probably be called here 
     } 
    }; 
} 

此作品,未經長按被檢測到,並且onLongPress方法被調用的問題,但是我在顯示ContextMenu時不知所措。我試着這樣做通常的方式在我的活動:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv); 
    registerForContextMenu(mwv); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
        ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.context, menu); 
} 

然而,當我在模擬器中長按該MyWebView,沒有任何反應。我需要從onPressPress()調用來顯示ContextMenu?

回答

1

在onLongPress中調用Activity.openContextMenu(View v)。這意味着讓MyWebView保留對Activity的引用。

3

基於gngr44的建議,我現在開始工作了。我讓我的活動實現了OnLongClickListener類,並提供了一個打開上下文菜單的onLongClick()方法。

修改後的代碼:

自定義的WebView:

public class MyWebView extends WebView { 
    MyActivity theListener; 
    Context context; 
    GestureDetector gd; 

    public MyWebView(Context context, AttributeSet attributes) { 
     super(context, attributes); 
     this.context = context; 
     gd = new GestureDetector(context, sogl); 
    } 

    // This is new 
    public void setListener(MyActivity l) { 
     theListener = l; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return gd.onTouchEvent(event); 
    } 

    GestureDetector.SimpleOnGestureListener sogl = 
       new GestureDetector.SimpleOnGestureListener() { 

     public boolean onDown(MotionEvent event) { 
      return true; 
     } 

     public void onLongPress(MotionEvent event) { 
      theListener.onLongClick(MyWebView.this); 
     } 
    }; 
} 

我的活動:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv); 
    registerForContextMenu(mwv); 
} 

public boolean onLongClick(View v) { 
    openContextMenu(v); 
    return true; 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
        ContextMenu.ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.context, menu); 
} 
+2

這使上下文菜單起作用。但是,我無法再滾動webview。實際上,任何觸摸都會彈出上下文菜單。任何想法如何使它perperly工作? – newman 2011-11-15 05:30:44

+1

同樣在這裏,webview失去了滾動能力 – KingFu 2013-04-09 12:57:33

0

我注意到,長按在模擬器任何需要的大量按5-7秒,而不是現實生活中的常規1-2。確保你按下至少10秒鐘,否則看起來沒有任何反應。

2

而不是從您的視圖訪問活動,我會建議在您的視圖中使用接口,並從您的活動實現該接口。

public class MyWebView extends WebView { 
    private OnLongPressListener mListener; 

    public MyWebView(Context context, AttributeSet attributes) { 
     mListener = (OnLongPressListener) context; 
    } 

    public void onLongPress(MotionEvent event) { 
     mListener.onLongPress(your variables); 
    } 

    public interface OnLongPressListener { 
     public void onLongPress(your variables); 
    } 
} 

public class YourActivity extends Activity implements OnLongPressListener { 

    @Override 
    public void onLongPress(your variables) { 
     // handle the longPress in your activity here: 
    } 
} 
相關問題