因此,我有一个EditText视图,我的想法是,让我们暂时说一下触摸事件以选择并将触摸的单词复制到剪贴板存储器中。到目前为止,我可以通过offset设置Selection,但这仅是""
因为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()
时调用。substring
selectedText
例如:
String substringFromStartSelection = getText().toString().substring (getSelectionStart());
int nextSpaceIndex = substringFromStartSelection.indexOf(" ");
int selectionEnd = getSelectionStart() + nextSpaceIndex;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句