2011-12-23 19 views
16

如何在Android中的TextView/EditText中選擇單詞上的水龍頭。我在TextView/EditText中有一些文本,當用戶點擊一個單詞時,我希望選擇該單詞,之後當我調用getSelectedText()方法時,它應該返回給我選定的單詞。 任何幫助,將不勝感激。在TextView/EditText中在水龍頭上選擇一個單詞

我的目標是在用戶點擊TextView/EditText中的某個單詞時執行某些操作。

回答

29

UPDATE:另一個更好的辦法是使用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

我想辦理點擊我自己的活動。我通過以下代碼解決了問題:

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

如果您正在尋找聽衆解決方案而不是意圖,這是最好的答案。並非所有問題都與URL或架構相關聯。 在我的情況下,我正在尋找一個簡單的解決方案,以瞭解哪個單詞在textview中被點擊。直到我看到這個,沒有好的解決方案。這個解決方案完美地工作。它與textview可點擊的功能相同,但最好的部分是我們可以控制採取相關操作。儘管我對上面的代碼做了一些修改。通過上面的代碼,所有單詞都顯示爲鏈接,但我不想要那樣。所以剛剛評論了super.updateDrawState。 – 2012-08-10 09:25:45

+0

非常感謝你,我從過去2天開始研究這個問題,最後我通過使用你的代碼解決了這個問題。再次感謝你。 – 2013-03-20 17:15:14

+1

謝謝M-WaJeEh。像魅力一樣工作。同時也感謝Deepak對我所追求的那點補充。 – Manu 2013-12-16 15:57:58

0

使用Linkify,使用您自己的正則表達式來調用您的例程在一個單詞的點擊。

看到這裏的WikiWord Example and Relevant Blogpost

+0

優秀。但我想通過監聽器處理點擊事件,而不是通過Intents :) – 2011-12-24 06:01:50

相關問題