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

我目前正在尝试获取一个自定义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时我不知所措.我尝试在我的Activity中按照惯例进行操作:

@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?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值