【发布时间】:2018-11-16 15:22:17
【问题描述】:
是否有 API 可以在 textview 中设置开始和结束选择(索引),以便它可以调用 CustomSelectionActionMode?p>
根据文档,Selection 类有一个 SetSelection 函数,它接受 3 个参数:spannable、start 和 end。但是如何从 TextView 类中检索 Selection 类呢?
【问题讨论】:
是否有 API 可以在 textview 中设置开始和结束选择(索引),以便它可以调用 CustomSelectionActionMode?p>
根据文档,Selection 类有一个 SetSelection 函数,它接受 3 个参数:spannable、start 和 end。但是如何从 TextView 类中检索 Selection 类呢?
【问题讨论】:
这就是答案,
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;
}
});
【讨论】:
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 函数中设置“选择/突出显示”。
【讨论】: