我已经使用this guide from Google和this tutorial生成了我自己的上下文操作栏。
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.annotation_menu, menu);
return true;
}
// Called each time the action mode is shown.
// Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.custom_button:
// do some stuff
break;
case R.id.custom_button2:
// do some other stuff
break;
default:
// This essentially acts as a catch statement
// If none of the other cases are true, return false
// because the action was not handled
return false;
}
finish(); // An action was handled, so close the CAB
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
此菜单设计为在用户选择文本时显示,因此它会覆盖本机的复制/粘贴菜单。现在我开始讨论我的问题了。
因为我覆盖了用于文本选择的函数,所以我还向WebView
添加了一个LongClickListener
并实现了onLongClick(View v)
方法,这样我就可以检测用户何时做出选择。
myWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mActionMode != null) {
return false;
}
mActionMode = startActionMode(mActionModeCallback);
v.setSelected(true);
return true;
}
});
当我长点击时,我看到我的自定义菜单出现,但没有文本突出显示。
我需要有文本选择功能;没有它,我的菜单就没有意义了。
如何覆盖onLongClick(View v)
,同时保持Android提供的文本选择?
如果这是不可能的,我是否可以调用其他地方的startActionMode(mActionModeCallback)
,这样文本将被正常选择,但我的自定义菜单也会出现?
如果这两个都不可能...帮助。
https://stackoverflow.com/questions/22336903
复制相似问题