2013-05-12 66 views
7

我正在創建一個小型Android應用程序。我想在一個textview中顯示一個文本,其中有多個部分點擊。 (每個應顯示一些不同的消息)Android在文本視圖中可多次點擊的字符串

最後,我設法找出如何在一個textview中顯示多個跨度,但不幸的是,onClick方法不起作用。根本沒有任何反應,甚至沒有logcat線。

我有這樣的事情:

SpannableStringBuilder ssb=new SpannableStringBuilder(); 
ssb.append("first second") 

ssb.setSpan(new ClickableSpan() { 
    @Override 
    public void onClick(View v) { 
    //Eredmeny2.this is just the context, name of the whole class 
    Toast.makeText(Eredmeny2.this, "first", Toast.LENGTH_LONG).show(); 
    } 
}, 1, 3, 0); 

ssb.setSpan(new ClickableSpan() { 
    @Override 
    public void onClick(View v) { 
    Toast.makeText(Eredmeny2.this, "second", Toast.LENGTH_LONG).show(); 
    } 
}, 7, 10, 0); 

TextView t1=new TextView(this); 
t1.setText(ssb); 
... 

的帶下劃線的文字很好,但是當我點擊他們沒有任何反應。 它是TableView的一部分,但我不認爲這是相關的。你有什麼想法,爲什麼它不做任何事情?我錯過了什麼?或者我應該以完全不同的方式做到這一點?

在此先感謝。


這部分將使用佈局文件如下:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
    android:id="@+id/ScrollView01" 
    android:background="#FF0000"> 


    <TableLayout 
     android:id="@+id/TableLayout01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:stretchColumns="0" 
     android:showDividers="middle" 
     android:padding="3dp"> 
    <TableRow 
     android:id="@+id/TableRow01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/TextView01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="3dp" 
      android:background="#000000" 
      android:textColor="#FFFFFF" 
      android:padding="6dp" 
      android:text="Hour" 
      android:textSize="20sp" 
      android:textStyle="bold" > 

     </TextView> 
     <TextView android:id="@+id/TextView02" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="20sp" 
      android:textStyle="bold" 
      android:text="Minute" 
      android:padding="6dp" 
      android:textColor="#FFFFFF" 
      android:background="#000000"> 
     </TextView> 

    </TableRow> 

    </TableLayout> 


</ScrollView> 

這TextView的直接使用TextView的佈局如下:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tv" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="10dp" 
android:textSize="16sp" 
android:background="#000000" 
android:textIsSelectable="false" 
android:textColor="#FFFFFF"> 
</TextView> 
+0

您可以發佈您的佈局文件? – Noundla 2013-05-12 18:57:40

+0

當然,在這裏你去,在更新的問題。 – skandigraun 2013-05-12 19:05:38

+0

將其更改爲true ... android:textIsSelectable =「true」 – Noundla 2013-05-12 19:09:44

回答

15

因爲我知道你想使textview的多個部分可點擊。

此代碼適合我!

SpannableString ss = new SpannableString("this is a text"); 
ss.setSpan(new myClickableSpan(1),0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
ss.setSpan(new myClickableSpan(2),5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
ss.setSpan(new myClickableSpan(3),8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

mTextView.setText(ss); 
mTextView.setMovementMethod(LinkMovementMethod.getInstance()); 

只是做定製ClickableSpan處理click事件

public class myClickableSpan extends ClickableSpan{ 

    int pos; 
    public myClickableSpan(int position){ 
     this.pos=position; 
    } 

    @Override 
    public void onClick(View widget) { 
     Toast.makeText(getApplicationContext(), "Position " + pos + " clicked!", Toast.LENGTH_LONG).show(); 
    } 

} 
+0

謝謝。這一個很好從其他答案。 – 2016-06-20 11:49:10

+0

簡單優雅的答案,非常有幫助!已經向上投票了。@ Akbar – TapanHP 2016-07-07 06:29:18

相關問題