在TextView/EditText中选择一个单词

M-W*_*eEh 17 android textview android-edittext spannable

如何在Android 中的TextView/ EditText中选择一个单词.我在TextView/中有一些文本,EditText当用户点击一个单词时,我希望选择该单词,之后当我调用getSelectedText()方法时,它应该返回所选单词.任何帮助,将不胜感激.

我的目标是在用户点击TextView/中的特定单词时执行一些操作EditText.

M-W*_*eEh 33

更新:另一种更好的方法是使用BreakIterator:

private void init() {
    String definition = "Clickable words in text view ".trim();
    TextView definitionView = (TextView) findViewById(R.id.text);
    definitionView.setMovementMethod(LinkMovementMethod.getInstance());
    definitionView.setText(definition, BufferType.SPANNABLE);
    Spannable spans = (Spannable) definitionView.getText();
    BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
    iterator.setText(definition);
    int start = iterator.first();
    for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
            .next()) {
        String possibleWord = definition.substring(start, end);
        if (Character.isLetterOrDigit(possibleWord.charAt(0))) {
            ClickableSpan clickSpan = getClickableSpan(possibleWord);
            spans.setSpan(clickSpan, start, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

private ClickableSpan getClickableSpan(final String word) {
    return new ClickableSpan() {
        final String mWord;
        {
            mWord = word;
        }

        @Override
        public void onClick(View widget) {
            Log.d("tapped on:", mWord);
            Toast.makeText(widget.getContext(), mWord, Toast.LENGTH_SHORT)
                    .show();
        }

        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

老答复

我想处理我自己的Activity中的click.我通过以下代码解决了它:

private void init(){
    String definition = "Clickable words in text view ".trim();
    TextView definitionView = (TextView) findViewById(R.id.definition);
    definitionView.setMovementMethod(LinkMovementMethod.getInstance());
    definitionView.setText(definition, BufferType.SPANNABLE);

    Spannable spans = (Spannable) definitionView.getText();
    Integer[] indices = getIndices(
            definitionView.getText().toString(), ' ');
    int start = 0;
    int end = 0;
      // to cater last/only word loop will run equal to the length of indices.length
    for (int i = 0; i <= indices.length; i++) {
        ClickableSpan clickSpan = getClickableSpan();
       // to cater last/only word
        end = (i < indices.length ? indices[i] : spans.length());
        spans.setSpan(clickSpan, start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        start = end + 1;
    }
}
private ClickableSpan getClickableSpan(){
     return new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                TextView tv = (TextView) widget;
                String s = tv
                        .getText()
                        .subSequence(tv.getSelectionStart(),
                                tv.getSelectionEnd()).toString();
                Log.d("tapped on:", s);
            }

            public void updateDrawState(TextPaint ds) {
                 super.updateDrawState(ds);
            }
        };
}
public static Integer[] getIndices(String s, char c) {
    int pos = s.indexOf(c, 0);
    List<Integer> indices = new ArrayList<Integer>();
    while (pos != -1) {
        indices.add(pos);
        pos = s.indexOf(c, pos + 1);
    }
    return (Integer[]) indices.toArray(new Integer[0]);
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您正在查看侦听器解决方案而非意图,这是最佳答案.并非所有问题都与URL或架构相关联.就我而言,我正在寻找一个简单的解决方案来了解在textview中使用哪个单词.直到我看到这个都没有好的解决方案.这个解决方案完美无缺.它与textview可点击的内容完全相同,但最好的部分是我们可以控制采取相关行动.我对上面的代码做了一处改动.使用上面的代码,所有单词都显示为链接,但我不希望这样.所以只是评论了super.updateDrawState. (3认同)
  • @ KK_07k11A0585不,你不能使用`getLineInstance()`,将http://stackoverflow.com/a/6726707/1112882与http://stackoverflow.com/a/6273546/1112882结合起来,以实现您的目标. (2认同)