1

我在我的应用程序中使用了一些自定义 ActionMode。当一个动作模式关闭时,我会做一些家务,比如关闭相关视图、更新更改等。我在 OnDestroyActionMode 中检测到该动作模式已关闭。

我的问题是,当在我的一些动作模式中时,用户可能会触发另一个系统动作模式(文本复制/粘贴/选择)。在这种情况下,onDestroyActionMode 被调用,我错误地认为用户完成了第一个动作模式,而不是实现“堆栈”功能,所以我可以忽略这个 onDestroyActionMode,让用户编辑/剪切/等文本,然后重新打开完成后的前动作模式。

我怎样才能做到这一点?

4

1 回答 1

0

进一步阐明您的情况:在蜂窝之前,TextView 上的 longPress 将产生一个带有选项的弹出窗口(如“选择单词”、“全选”和“将“某些单词”添加到字典中),而不会影响任何现有的 ActionMode显示和关闭时(通过按回)。所以这不是蜂窝前的问题。

关于 HTC Sense 的更多信息: Sense 不支持 TextView.setCustomSelectionActionModeCallback() 因为 Sense 不使用 ActionMode 来进行文本选择功能(显然不关心世界其他地方是否这样做!)。所以这个问题在那种情况下有不同的味道(我没有在 Sense 下测试过以下解决方案,所以不确定它会如何表现)。

一种解决方案是创建您自己的自定义 ActionMode.Callback 来替换操作系统的,并将其应用到您想要的任何 TextView 和/或 EditText 的 setCustomSelectionActionModeCallback() 中(尽管仅当设备运行蜂窝或更高版本时)。将自定义的 onTextSelectionCABDestroyed 回调接口传递给您自定义的 ActionMode.Callback,在 onDestroyActionMode 方法中调用它。

首先创建一个接口并在您想要处理原始 ActionMode 的重新创建的地方实现它(或者您可能想要使用带有 Otto 之类的总线事件):

public interface YourCallbackInterface {
    public void onTextSelectionCABDestroyed();
}

并创建一个新类:

public final class CustomTextSelectionActionModeCallback implements ActionMode.Callback {
WeakReference<YourCallbackinterface> mYourCallbackinterface;
public CustomTextSelectionActionModeCallback(YourCallbackinterface yourCallbackInterface) {
    mYourCallbackinterface = new WeakReference<YourCallbackinterface>(yourCallbackInterface);
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    return true;    //returning true will create the ActionMode
}
@Override
public void onDestroyActionMode(ActionMode mode) {
    //this is the magic where we actually capture the destroy event for TextSelectionCAB and can subsequently do things like recreate the ActionMore that TextSelectionCAB greedily destroyed!
    mYourCallbackinterface.get().onTextSelectionCABDestroyed();
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
}

}

并记住在从 ActionMode 的 onDestroyActionMode 重新创建 ActionMode 时避免 StackOverflowException,将 Runnable 延迟到像这样的处理程序我在这里解释:Reopen ActionMode (or CAB) after onDestroyActionMode is called

最后,如果您使用的是 ActionBarSherlock,请确保您的 CustomTextSelectionActionModeCallback 实现 android.view.ActionMode.Callback 而不是 com.actionbarsherlock.view.ActionMode.Callback。

注意:我没有玩过 ActionBarCompat,所以不确定这一切如何适用。如果有人知道,请发表评论!

于 2013-10-01T20:51:47.200 回答