2013-05-12 64 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

相关问题