select a word on a tap in TextView/EditText

我怕爱的太早我们不能终老 提交于 2019-11-26 04:42:48

问题


How to select a word on a tap in TextView/EditText in android. I have some text in a TextView/EditText and when user taps on a word, I want that word to be selected and after that when I call getSelectedText() like method, it should return me the selected word. Any Help would be appreciated.

My goal is to perform some action when user taps on a particular word in TextView/EditText.


回答1:


UPDATE: Another better approach is to use 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);
        }
    };
}

OLD ANSWER

I wanted to handle click in my own Activity. I solved it by following code:

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]);
}



回答2:


Use Linkify, Use your own Regular Expression to invoke your routine on the click of a word.

See here for the WikiWord Example and Relevant Blogpost



来源:https://stackoverflow.com/questions/8612652/select-a-word-on-a-tap-in-textview-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!