39

我有一个应用程序,我希望能够显示一个允许用户选择一些文本的 TextView(或 EditText),然后按一个按钮来处理该文本。在 Honeycomb 之前的 Android 版本上实现这一点没有问题,但在 Honeycomb 及更高版本上,默认的长按操作是显示带有复制/剪切/粘贴选项的操作栏。我可以截取长按以显示我自己的操作栏,但是我没有显示文本选择句柄。

一旦我启动了自己的 ActionMode,我如何才能显示文本选择句柄?

这是我用来启动 ActionMode 的代码,除了没有显示文本选择句柄外,它可以工作:

public boolean onLongClick(View v) {
    if(actionMode == null)
        actionMode = startActionMode(new QuoteCallback());
    return true;
}

class QuoteCallback implements ActionMode.Callback {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.quote, menu);
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch(item.getItemId()) {

        case R.id.quote:
            Log.d(TAG, "Selected menu");
            mode.finish();
            // here is where I would grab the selected text
            return true;
        }
        return false;
    }

    public void onDestroyActionMode(ActionMode mode) {
        actionMode = null;
    }
}
4

3 回答 3

57

我想出了自己问题的答案;TextView(以及因此 EditText)有一个setCustomSelectionActionModeCallback()应该用来代替startActionMode(). 使用它可以自定义 TextView 用于文本选择的菜单。示例代码:

bodyView.setCustomSelectionActionModeCallback(new StyleCallback());

其中 StyleCallback 通过删除 Select All 并添加一些样式操作来自定义文本选择菜单:

class StyleCallback implements ActionMode.Callback {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        Log.d(TAG, "onCreateActionMode");
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.style, menu);
        menu.removeItem(android.R.id.selectAll);
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        Log.d(TAG, String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
        CharacterStyle cs;
        int start = bodyView.getSelectionStart();
        int end = bodyView.getSelectionEnd();
        SpannableStringBuilder ssb = new SpannableStringBuilder(bodyView.getText());

        switch(item.getItemId()) {

        case R.id.bold:
            cs = new StyleSpan(Typeface.BOLD);
            ssb.setSpan(cs, start, end, 1);
            bodyView.setText(ssb);
            return true;

        case R.id.italic:
            cs = new StyleSpan(Typeface.ITALIC);
            ssb.setSpan(cs, start, end, 1);
            bodyView.setText(ssb);
            return true;

        case R.id.underline:
            cs = new UnderlineSpan();
            ssb.setSpan(cs, start, end, 1);
            bodyView.setText(ssb);
            return true;
        }
        return false;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }
}

菜单添加的 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/italic"
          android:showAsAction="always"
          android:icon="@drawable/italic"
          android:title="Italic"/>
    <item android:id="@+id/bold"
          android:showAsAction="always"
          android:icon="@drawable/bold"
          android:title="Bold"/>
    <item android:id="@+id/underline"
          android:showAsAction="always"
          android:icon="@drawable/underline"
          android:title="Underline"/>
</menu>
于 2012-10-22T03:44:29.843 回答
10

如果您想自定义操作栏中的选项,上述解决方案很好。但是如果你想覆盖操作栏复制/粘贴等,下面是代码......

public class MainActivity extends Activity {
    EditText editText;
    private ClipboardManager myClipboard;
    private ClipData myClip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        editText = (EditText) findViewById(R.id.editText3);

        myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        editText = (EditText) findViewById(R.id.editText3);
        editText.setCustomSelectionActionModeCallback(new Callback() {

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // TODO Auto-generated method stub
                switch (item.getItemId()) {
                case android.R.id.copy:
                    int min = 0;
                    int max = editText.getText().length();
                    if (editText.isFocused()) {
                        final int selStart = editText.getSelectionStart();
                        final int selEnd = editText.getSelectionEnd();

                        min = Math.max(0, Math.min(selStart, selEnd));
                        max = Math.max(0, Math.max(selStart, selEnd));
                    }
                    // Perform your definition lookup with the selected text
                    final CharSequence selectedText = editText.getText()
                            .subSequence(min, max);
                    String text = selectedText.toString();

                    myClip = ClipData.newPlainText("text", text);
                    myClipboard.setPrimaryClip(myClip);
                    Toast.makeText(getApplicationContext(), "Text Copied",
                            Toast.LENGTH_SHORT).show();
                    // Finish and close the ActionMode
                    mode.finish();
                    return true;
                case android.R.id.cut:
                    // add your custom code to get cut functionality according
                    // to your requirement
                    return true;
                case android.R.id.paste:
                    // add your custom code to get paste functionality according
                    // to your requirement
                    return true;

                default:
                    break;
                }
                return false;
            }
        });         
    }    
}
于 2015-05-08T07:03:07.567 回答
0

最简单的方法是在您的application标签中定义的主题样式中添加一行AndroidManifest。打开您的主题样式并添加以下内容:

<item name="actionModeBackground">@color/your_color</item>

或者

<item name="android:actionModeBackground">@color/your_color</item>

例如:我定义的主题风格:

<style name="AppTheme" parent="AppBaseTheme">

        <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
        <item name="android:actionBarStyle">@style/AppTheme1</item>
        <!-- below is the line you have to add -->
        <item name="android:actionModeBackground">@color/black_actionBar</item>
</style>
于 2016-02-11T08:16:41.117 回答