所以,我有一个 EditText View ,我的想法是,让我们说现在,触摸事件来选择触摸的单词并将其复制到剪贴板内存中。到目前为止我可以通过偏移设置选择,但它只是""
,因为getSelectionStart() = getSelectionEnd() = offset
。我怎样才能将它扩展到下一个遇到空格或文本结束/开始。
如何以上述方式扩展这样的选择?
到目前为止的代码:
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getX() + getScrollX();
int offset = getOffset(event);
if(offset != Integer.MIN_VALUE){
setSelection(offset);
String selectedText = getText().toString()
.substring(getSelectionStart(), getSelectionEnd());
//TODO: extend selection otherwise copies ""
putInClipMemory(selectedText);
}
return true;
}
谢谢。
最佳答案
您应该能够计算开始选择索引之后的下一个空格的索引,并使用它(在我的示例中下面的 endIndex
)而不是 getSelectionEnd()
时在设置 selectedText
时调用 substring
。
例如:
String substringFromStartSelection = getText().toString().substring (getSelectionStart());
int nextSpaceIndex = substringFromStartSelection.indexOf(" ");
int selectionEnd = getSelectionStart() + nextSpaceIndex;
关于java - 如何在 EditText 中将 Selection 扩展到整个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912921/