7

我是android编程的新手。我在edittext中添加了一个上下文菜单。我希望长按光标下的单词。

我可以通过以下代码获取选定的文本。

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    EditText edittext = (EditText)findViewById(R.id.editText1);
    menu.setHeaderTitle(edittext.getText().toString().substring(edittext.getSelectionStart(), edittext.getSelectionEnd()));
    menu.add("Copy");
}

edittext 有一些文本,例如“一些文本。更多文本”。当用户点击“更多”时,光标会在“更多”字样的某个地方。当用户长按单词时,我想在光标下获取单词“更多”和其他单词。

4

5 回答 5

8

有更好更简单的解决方案:在 android 中使用模式

public String getCurrentWord(EditText editText) {
    Spannable textSpan = editText.getText();
    final int selection = editText.getSelectionStart();
    final Pattern pattern = Pattern.compile("\\w+");
    final Matcher matcher = pattern.matcher(textSpan);
    int start = 0;
    int end = 0;

    String currentWord = "";
    while (matcher.find()) {
        start = matcher.start();
        end = matcher.end();
        if (start <= selection && selection <= end) {
            currentWord = textSpan.subSequence(start, end).toString();
            break;
        }
    }

    return currentWord; // This is current word
}
于 2014-09-16T23:39:04.080 回答
6
EditText et = (EditText) findViewById(R.id.xx);

int startSelection = et.getSelectionStart();

String selectedWord = "";
int length = 0;

for(String currentWord : et.getText().toString().split(" ")) {
    System.out.println(currentWord);
    length = length + currentWord.length() + 1;
    if(length > startSelection) {
        selectedWord = currentWord;
        break;
    }
}

System.out.println("Selected word is: " + selectedWord);
于 2013-08-14T08:27:16.353 回答
3

请尝试以下代码,因为它已经过优化。如果您有更多规格,请告诉我。

//String str = editTextView.getText().toString(); //suppose edittext has "Hello World!" 
int selectionStart = editTextView.getSelectionStart(); // Suppose cursor is at 2 position
int lastSpaceIndex = str.lastIndexOf(" ", selectionStart - 1);
int indexOf = str.indexOf(" ", lastSpaceIndex + 1);
String searchToken = str.substring(lastSpaceIndex + 1, indexOf == -1 ? str.length() : indexOf);

Toast.makeText(this, "Current word is :" + searchToken, Toast.LENGTH_SHORT).show();
于 2014-09-12T10:37:30.750 回答
3

我相信 aBreakIterator是这里最好的解决方案。它避免了遍历整个字符串并自己进行模式匹配。除了简单的空格字符(逗号、句点等)之外,它还可以找到单词边界。

// assuming that only the cursor is showing, no selected range
int cursorPosition = editText.getSelectionStart();

// initialize the BreakIterator
BreakIterator iterator = BreakIterator.getWordInstance();
iterator.setText(editText.getText().toString());

// find the word boundaries before and after the cursor position
int wordStart;
if (iterator.isBoundary(cursorPosition)) {
    wordStart = cursorPosition;
} else {
    wordStart = iterator.preceding(cursorPosition);
}
int wordEnd = iterator.following(cursorPosition);

// get the word
CharSequence word = editText.getText().subSequence(wordStart, wordEnd);

如果您想长按它,那么只需将其放入onLongPress您的GestureDetector.

也可以看看

于 2017-06-17T08:37:48.950 回答
0

@Ali 感谢您提供解决方案。

这是一个优化的变体,如果找到了这个词,它会中断。

此解决方案不会创建 Spannable,因为不需要查找单词。

@NonNull
public static String getWordAtIndex(@NonNull String text, @IntRange(from = 0) int index) {
    String wordAtIndex = "";

    // w = word character: [a-zA-Z_0-9]
    final Pattern pattern = Pattern.compile("\\w+");
    final Matcher matcher = pattern.matcher(text);

    int startIndex;
    int endIndex;

    while (matcher.find()) {
        startIndex = matcher.start();
        endIndex = matcher.end();

        if ((startIndex <= index) && (index <= endIndex)) {
            wordAtIndex = text.subSequence(startIndex, endIndex).toString();
            break;
        }
    }
    return wordAtIndex;
}

示例:获取当前光标位置的单词:

String text = editText.getText().toString();
int cursorPosition = editText.getSelectionStart();

String wordAtCursorPosition = getWordAtIndex(text, cursorPosition);

如果要查找所有连接字符(包括标点符号),请改用此方法:

// S = non-whitespace character: [^\s]
final Pattern pattern = Pattern.compile("\\S+");

Java正则表达式文档(正则表达式):https ://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

于 2016-12-10T16:33:27.933 回答