对于自定义的EditText剪切/复制操作栏中显示的文本选择手柄 [英] Custom cut/copy action bar for EditText that shows text selection handles

查看:943
本文介绍了对于自定义的EditText剪切/复制操作栏中显示的文本选择手柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我希望能够表现出一个TextView(或EditText上),它允许用户选择一些文本,然后$​​ P $ PSS一个按钮,有一些与文字完成。之前的蜂窝实现这个在Android版本中是没有问题的,但是,蜂窝及以上默认长preSS行动,以​​显示与复制/剪切/粘贴选项的操作栏。我可以拦截长期preSS展现我自己的行动吧,但我不明白的文字选择手柄显示。

一旦我开始了我自己的ActionMode如何获取文本选择手柄显示?

下面是code我使用启动ActionMode,它的工作原理只是没有文本选择手柄显示:

 公共布尔onLongClick(视图v){
    如果(actionMode == NULL)
        actionMode = startActionMode(新QuoteCallback());
    返回true;
}

类QuoteCallback实现ActionMode.Callback {

    公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
        MenuInflater充气= mode.getMenuInflater();
        inflater.inflate(R.menu.quote,菜单);
        返回true;
    }

    在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
        返回false;
    }

    公共布尔onActionItemClicked(ActionMode模式,菜单项项){
        开关(item.getItemId()){

        案例R.id.quote:
            Log.d(TAG,选择菜单);
            mode.finish();
            //这里是我会抓住选定的文本
            返回true;
        }
        返回false;
    }

    公共无效onDestroyActionMode(ActionMode模式){
        actionMode = NULL;
    }
}
 

解决方案

我想出答案我自己的问题; TextView的(因此EditText上)有一个方法 setCustomSelectionActionModeCallback()应用来代替 startActionMode()。使用这种能够对文本选择所用的TextView菜单的定制。样品code:

  bodyView.setCustomSelectionActionModeCallback(新StyleCallback());
 

在这里StyleCallback customises文本选择菜单中去除所有的选择并增加了一些造型动作:

 类StyleCallback实现ActionMode.Callback {

    公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
        Log.d(TAG,onCreateActionMode);
        MenuInflater充气= mode.getMenuInflater();
        inflater.inflate(R.menu.style,菜单);
        menu.removeItem(android.R.id.selectAll);
        返回true;
    }

    在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
        返回false;
    }

    公共布尔onActionItemClicked(ActionMode模式,菜单项项){
        Log.d(TAG,的String.Format(onActionItemClicked项目=%s的/%D,item.toString(),item.getItemId()));
        CharacterStyle CS;
        INT开始= bodyView.getSelectionStart();
        INT端= bodyView.getSelectionEnd();
        SpannableStringBuilder SSB =新SpannableStringBuilder(bodyView.getText());

        开关(item.getItemId()){

        案例R.id.bold:
            CS =新StyleSpan(Typeface.BOLD);
            ssb.setSpan(CS,开始,结束,1);
            bodyView.setText(SSB);
            返回true;

        案例R.id.italic:
            CS =新StyleSpan(Typeface.ITALIC);
            ssb.setSpan(CS,开始,结束,1);
            bodyView.setText(SSB);
            返回true;

        案例R.id.underline:
            CS =新UnderlineSpan();
            ssb.setSpan(CS,开始,结束,1);
            bodyView.setText(SSB);
            返回true;
        }
        返回false;
    }

    公共无效onDestroyActionMode(ActionMode模式){
    }
 

中的XML的菜单添加为:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID /斜体
          机器人:showAsAction =总是
          机器人:图标=@可绘制/斜体
          机器人:标题=斜体/>
    <项目机器人:ID =@ + ID /大胆
          机器人:showAsAction =总是
          机器人:图标=@可绘制/大胆
          机器人:标题=大胆/>
    <项目机器人:ID =@ + ID /下划线
          机器人:showAsAction =总是
          机器人:图标=@可绘制/下划线
          机器人:标题=下划线/>
< /菜单>
 

I have an app where I want to be able to show a TextView (or EditText) that allows the user to select some text, then press a button to have something done with that text. Implementing this on Android versions prior to Honeycomb is no problem but on Honeycomb and above the default long-press action is to show an action bar with Copy/Cut/Paste options. I can intercept long-press to show my own action bar, but then I do not get the text selection handles displayed.

Once I have started my own ActionMode how do I get the text selection handles displayed?

Here is the code I'm using to start the ActionMode, which works except there are no text selection handles displayed:

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;
    }
}

解决方案

I figured out the answer to my own question; TextView (and therefore EditText) has a method setCustomSelectionActionModeCallback() which should be used instead of startActionMode(). Using this enables customisation of the menu used by TextView for text selection. Sample code:

bodyView.setCustomSelectionActionModeCallback(new StyleCallback());

where StyleCallback customises the text selection menu by removing Select All and adding some styling actions:

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) {
    }

The XML for the menu additions is:

<?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>

这篇关于对于自定义的EditText剪切/复制操作栏中显示的文本选择手柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆