webview长按弹出菜单

2012-01-12 16:51

android webview 长按事件处理

webview 中可以增加长按监听事件来弹出菜单。

具体实现下面这个函数即可:

//webview 长按事件
webView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){

public void onCreateContextMenu(ContextMenu menu, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// do the menu action
switch(item.getItemId()){
case WEB_FenXiang:
Log.i("you click","分享");
break;
case WEB_XiaZai:
Log.i("you click","下载");
break;
case WEB_FuZhi:
Log.i("you click","复制");
break;
case WEB_AddSQ:
Log.i("you click","增加到书签");
break;
}
return true;
}
};
Log.i("long click","true");
HitTestResult result = ((WebView) arg1).getHitTestResult();
int resultType = result.getType();
if ((resultType == HitTestResult.ANCHOR_TYPE) ||
(resultType == HitTestResult.IMAGE_ANCHOR_TYPE) ||
(resultType == HitTestResult.SRC_ANCHOR_TYPE) ||
(resultType == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)) {
Intent i = new Intent();
Log.i("extrea",result.getExtra());
MenuItem item = menu.add(0, WEB_FenXiang, 0, "分享").setOnMenuItemClickListener(handler);
item.setIntent(i);
item = menu.add(0, WEB_FuZhi, 0, "复制").setOnMenuItemClickListener(handler);
item.setIntent(i);

item = menu.add(0,WEB_XiaZai, 0,"下载").setOnMenuItemClickListener(handler); ;
item.setIntent(i);

item = menu.add(0,WEB_AddSQ, 0,"增加到书签").setOnMenuItemClickListener(handler); ;
item.setIntent(i);
menu.setHeaderTitle(result.getExtra());
}
else if (resultType == HitTestResult.IMAGE_TYPE) {
Log.i("image type","ture");
Intent i = new Intent();
MenuItem item = menu.add(0, 1, 0, "OPEN");
item.setIntent(i);
item = menu.add(0, 2, 0, "图片");
item.setIntent(i);
menu.setHeaderTitle(result.getExtra());
}
}
});

看下效果:

webview长按弹出菜单_第1张图片

你可能感兴趣的:(android)