是否有API可以在textview中设置开始和结束选择(索引),以便可以调用CustomSelectionActionMode? p>
根据文档,Selection类具有一个SetSelection函数,该函数带有3个参数:spannable,start和end。但是,如何从TextView类中检索Selection类呢?
答案 0 :(得分:0)
这是答案,
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;
}
});
答案 1 :(得分:0)
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;
}
}
这是用Xamarin.Android的C#编写的。我正在使用计时器来检测“ LongPress”,并希望在LongPress函数中设置“选中/突出显示”。