2011-02-03 36 views
5

Tôi hiện đang cố gắng để có một WebView tùy chỉnh hiển thị ContextMenu khi nó được nhấn trong một thời gian dài hơn. Như lớp WebView mặc định chỉ hiển thị một ContextMenu khi một liên kết được longPressed, tôi đã viết lớp của riêng tôi để ghi đè lên hành vi này:Android: Mở một ContextMenu từ onLongPress trong WebView tùy chỉnh

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 
     } 
    }; 
} 

này hoạt động mà không vấn đề nhấn và giữ được phát hiện và phương pháp onLongPress được gọi, tuy nhiên tôi bị mất khi nói đến việc hiển thị ContextMenu. Tôi đã thử thực hiện theo cách thông thường trong Hoạt động của mình:

@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); 
} 

Tuy nhiên, khi tôi nhấn LongMyWebView trong trình mô phỏng, không có gì xảy ra. Tôi phải gọi gì từ onLongPress() để hiển thị ContextMenu?

Trả lời

1

Hoạt động cuộc gọi.openContextMenu (Xem v) trong onLongPress. Điều này có nghĩa là có MyWebView giữ một tham chiếu đến Activity mặc dù.

3

Tôi làm việc ngay bây giờ, xây dựng dựa trên đề xuất của gngr44. Tôi đã thực hiện hoạt động của mình triển khai lớp OnLongClickListener và cung cấp phương thức onLongClick() mở menu ngữ cảnh.

Mã sửa đổi:

Các WebView tùy chỉnh:

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); 
     } 
    }; 
} 

Hoạt động của tôi:

@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

Điều này làm cho menu ngữ cảnh hoạt động. Tuy nhiên, tôi không thể cuộn webview được nữa. Trên thực tế, mọi liên lạc sẽ bật lên menu ngữ cảnh. Bất kỳ ý tưởng làm thế nào để làm cho nó hoạt động perperly? – newman

+1

Tương tự ở đây, webview mất khả năng cuộn của nó – KingFu

+0

Tôi nghĩ rằng đó là bởi vì bạn đã ghi đè onTouchEvent nhưng chưa triển khai siêu lớp. @Override công khai boolean onTouchEvent (sự kiện MotionEvent) { nếu (gestureDetector.onTouchEvent (sự kiện)) trả về true; trả lại super.onTouchEvent (sự kiện); } Mã cho nên làm các trick :) –

0

tôi nhận thấy rằng cách nhấn lâu bất cứ điều gì trong giả lập đòi hỏi một LOT của nhấn, như 5-7 giây như trái ngược với 1-2 thường xuyên trong cuộc sống thực. Hãy chắc chắn rằng bạn nhấn trong ít nhất 10 giây, nếu không nó sẽ có vẻ như không có gì xảy ra.

2

Thay vì truy cập vào hoạt động từ quan điểm của bạn tôi sẽ khuyên bạn sử dụng một giao diện trong View của bạn và thực hiện các giao diện từ hoạt động của bạn.

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: 
    } 
} 
+0

Hoặc kích hoạt một sự kiện sử dụng cái gì đó như EventBus. –

Các vấn đề liên quan