【问题标题】:Android TextView text selectionAndroid TextView 文本选择
【发布时间】:2018-11-16 15:22:17
【问题描述】:

是否有 API 可以在 textview 中设置开始和结束选择(索引),以便它可以调用 CustomSelectionActionMode?​​p>

根据文档,Selection 类有一个 SetSelection 函数,它接受 3 个参数:spannable、start 和 end。但是如何从 TextView 类中检索 Selection 类呢?

【问题讨论】:

    标签: textview android


    【解决方案1】:

    这就是答案,

    https://stackoverflow.com/a/22833303/1177865

    mTextView.setCustomSelectionActionModeCallback(new Callback() {
    
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // Remove the "select all" option
            menu.removeItem(android.R.id.selectAll);
            // Remove the "cut" option
            menu.removeItem(android.R.id.cut);
            // Remove the "copy all" option
            menu.removeItem(android.R.id.copy);
            return true;
        }
    
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Called when action mode is first created. The menu supplied
            // will be used to generate action buttons for the action mode
    
            // Here is an example MenuItem
            menu.add(0, DEFINITION, 0, "Definition").setIcon(R.drawable.ic_action_book);
            return true;
        }
    
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // Called when an action mode is about to be exited and
            // destroyed
        }
    
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case DEFINITION:
                    int min = 0;
                    int max = mTextView.getText().length();
                    if (mTextView.isFocused()) {
                        final int selStart = mTextView.getSelectionStart();
                        final int selEnd = mTextView.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 = mTextView.getText().subSequence(min, max);
                    // Finish and close the ActionMode
                    mode.finish();
                    return true;
                default:
                    break;
            }
            return false;
        }
    
    });
    

    【讨论】:

    • 我已经添加了 setCustomSelectionActionModeCallback。这部分代码最初是有效的,直到我添加了 Touch 监听器。显然,使用触摸侦听器,长按不再选择文本。因此,我的问题是如何手动设置 SelectionStart 和 SelectionEnd 以便触发 CustomSelectionActionModeCallback。
    • 您覆盖了触摸侦听器,并且控件无法像往常一样流动。你可以发布你的 ontouch 听众吗?我认为这个想法是通过使用 return false 继续将控制权传递给父级。 developer.android.com/reference/android/view/…。示例代码stackoverflow.com/questions/11690504/…
    【解决方案2】:
    bool LongPress()
    {
        if (longpressed == 1)
        {
        int x = (int)downX;
        int y = (int)downY;
        x -= textview.PaddingLeft;
        y -= textview.PaddingTop;
        x += textview.ScrollX;
        y += textview.ScrollY;
        Android.Text.Layout layout = textview.Layout;
        int line = layout.GetLineForVertical(y);
        int off = layout.GetOffsetForHorizontal(line, x);
        var clickspans = ss.GetSpans(off, off, Java.Lang.Class.FromType(typeof(ClickableSpan)));
        if (clickspans.Count() > 0)
        {
            ClickableSpan clickspan = (ClickableSpan)clickspans[0];
            startselection = ss.GetSpanStart(clickspan);
            endselection = ss.GetSpanEnd(clickspan);
            /*
            This is where I intend to add Selection.SetSelection(ss, startselection, endselection);
            */
            //textview.StartActionMode(textview.CustomSelectionActionModeCallback, ActionModeType.Floating);
        }
        longpressed = 2;
        }
        return false;
    }
    
    private void TouchLabel(object sender, TouchEventArgs e)
    {
        MotionEvent motionevt = e.Event;
        if (MotionEvent.ActionToString(motionevt.Action) == "ACTION_DOWN")
        {
        Device.StartTimer(TimeSpan.FromMilliseconds(500), LongPress);
        longpressed = 1;
        }
        else if (MotionEvent.ActionToString(motionevt.Action) == "ACTION_MOVE")
        {
        longpressed = 2;
        }
        else if ((MotionEvent.ActionToString(motionevt.Action) == "ACTION_UP") || (MotionEvent.ActionToString(motionevt.Action) == "ACTION_CANCEL"))
        {
        if (longpressed == 1)
        {
            longpressed = 2;
        }
        longpressed = 0;
        }
    }
    

    这是用 C# 为 Xamarin.Android 编写的。我正在使用计时器来检测“LongPress”,并想在 LongPress 函数中设置“选择/突出显示”。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2011-06-26
      • 2011-12-11
      • 2012-09-03
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多