Android:在自定义WebView中从onLongPress打开ContextMenu

时间:2011-02-03 02:06:42

标签: android webview contextmenu gesturedetector

我正在尝试获取一个自定义WebView,当它被按下较长时间时会显示一个ContextMenu。由于默认WebView类仅在链接为longPressed时显示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
        }
    };
}

这可以正常工作,检测到longPress并调用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时,没有任何反应。我需要从onLongPress()调用什么来显示ContextMenu?

4 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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:
    }
}

答案 2 :(得分:1)

在onLongPress中调用Activity.openContextMenu(View v)。这意味着让MyWebView保留对Activity的引用。

答案 3 :(得分:0)

我注意到在模拟器中长按任何东西需要很多按压,比如5-7秒,而不是现实生活中常规的1-2。确保按下至少10秒钟,否则看起来没有任何反应。