在TextView中Android的多串点击(Android Multiple clickable

2019-09-02 18:08发布

我创建一个小的Android应用程序。 我想显示在由多部分组成一个TextView文本点击。 (每个应显示一些不同的消息)

最后,我设法找到了如何在一个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>

Answer 1:

据我了解你想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();
    }

}


文章来源: Android Multiple clickable strings in textview