[英]How to override Contextual Action Bar when selecting text in a WebView in Android?
I've googled a lot, and find some tutorials and answers here in stackoverflow, but I am facing some difficults to resolve this issue.我在 google 上搜索了很多,并在 stackoverflow 中找到了一些教程和答案,但是我在解决这个问题时遇到了一些困难。
I have a Fragment with a WebView, and I want to show my custom Contextual Action Bar when the user selects some text of my web view.我有一个带有 WebView 的 Fragment,当用户选择我的 Web 视图的某些文本时,我想显示我的自定义上下文操作栏。 I have two main issues here:
我在这里有两个主要问题:
Some of my current code:我目前的一些代码:
Custom interface:自定义界面:
public class SelectActionModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
}
Custom WebView自定义网页视图
public class CustomWebView extends WebView {
private SelectActionModeCallback actionModeCallback;
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public ActionMode startActionMode(Callback callback) {
actionModeCallback = new SelectActionModeCallback();
return super.startActionMode(actionModeCallback);
}
In my fragment, I have this:在我的片段中,我有这个:
@Override
public void onResume() {
super.onResume();
myWebView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (mActionMode != null) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_SHORT).show();
return false;
}
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
private SelectActionModeCallback mActionModeCallback = new SelectActionModeCallback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.custom, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_sustom:
customMethod();
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
THERE IS A BETTER WAY!有一个更好的方法! See the following answer: https://stackoverflow.com/a/22391169/2608235
请参阅以下答案: https : //stackoverflow.com/a/22391169/2608235
OnLongClickListener
and keep the selection within a WebView
.
OnLongClickListener
并将选择保留在WebView
。
Android does some weird stuff behind the scenes for selecting text within WebViews
.
WebViews
文本。
If you override OnLongClickListener
in order to call startActionMode
, you will lose the selection, as you have found out.
OnLongClickListener
以调用startActionMode
,您将失去选择,正如您所发现的。
What you should do instead is override startActionMode
in your fragment, rather than its parent View
(in your case, CustomWebView
).你应该做的是在你的片段中覆盖
startActionMode
,而不是它的父View
(在你的情况下是CustomWebView
)。
I don't have the mod permission to mark this question as a duplicate, but it is.我没有将这个问题标记为重复的 mod 权限,但确实如此。
See my question for more info: Use a custom contextual action bar for WebView text selection有关更多信息,请参阅我的问题: Use a custom contextual action bar for WebView text selection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.