我正在自定义的WebView
.
我的要求是从第一个 CAB 中选择一个特定选项将打开第二个 CAB。这是最终目标:
我已经做到了,但选择无法正常工作。我知道它看起来是正确的,但是如果用户点击其他三个图标中的任何一个,结果如下:
如果用户在按下四个初始图标中的任何一个后触摸选择,则应用程序由于空指针异常而崩溃。
当用户在选择操作后触摸延迟选择时,应用程序会尝试访问ActionMode
(我们称之为firstActionMode
)创建带有图标的 CAB 以使其无效/刷新。但是,firstActionMode
在创建带有颜色的 CAB 时被破坏,(调用它secondActionMode
)导致空指针异常。
为了清除选择,我发现clearFocus()
在onDestroyActionMode(firstActionMode)
方法内调用可以很好地完成工作。
private class CustomActionModeCallback extends ActionMode.Callback {
@Override
public void onDestroyActionMode(ActionMode mode) {
clearFocus();
}
}
secondActionMode
但是,当实现这一点时,在创建其 CAB时选择不会持续存在:
从这一点选择颜色实际上会产生所需的功能。然而,虽然这“有效”,但它(最不幸的是)不符合我的要求。当显示由创建的 CAB 时,我需要选择保持可见和功能secondActionMode
。
这是我的自定义类 (
WebView
) 及其嵌套ActionMode
的代码
public class CustomWebView extends WebView {
private ActionMode.Callback mActionModeCallback;
@Override
public ActionMode startActionMode(Callback callback) {
ViewParent parent = getParent();
if (parent == null) {
return null;
}
if (callback instanceof HighlightActionModeCallback) {
mActionModeCallback = callback;
} else {
mActionModeCallback = new CustomActionModeCallback();
}
return parent.startActionModeForChild(this, mActionModeCallback);
}
private class CustomActionModeCallback implements 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.first_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) {
// This method is called when the handlebars are moved.
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.copy:
// Do stuff
break;
case R.id.bookmark:
// Do stuff
break;
case R.id.highlight:
startActionMode(new HighlightActionModeCallback());
break;
case R.id.note:
// Do stuff
break;
default:
return false;
}
mode.finish(); // Action picked, so close the CAB
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
clearFocus(); // This is commented in the first four screens.
}
}
private class HighlightActionModeCallback implements 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.highlight_colors, 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) {
// This method is called when the handlebars are moved.
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) {
mode.finish(); // Action picked, so close the CAB
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
// Remove the selection highlight and handles.
clearFocus();
}
}
}
如何解决这个问题?任何和所有的帮助表示赞赏。