【问题标题】:Android Multiple clickable strings in textviewAndroid textview中的多个可点击字符串
【发布时间】:2015-07-04 20:19:25
【问题描述】:

我正在创建一个小型 Android 应用。我想在一个文本视图中显示一个文本,其中包含多个要单击的部分。 (每个都应该显示一些不同的信息)

最后我设法找出如何在一个文本视图中显示多个跨度,但不幸的是 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>

【问题讨论】:

  • 你能发布你的布局文件吗?
  • 没问题,在更新的问题中。
  • 将此更改为 true... android:textIsSelectable="true"
  • 您可以从此链接获得答案。 stackoverflow.com/questions/5183645/… 试试看。
  • 为什么要把事情复杂化?多个文本视图有什么问题?

标签: java android onclick textview


【解决方案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 来处理点击事件

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

}

【讨论】:

  • 简单而优雅的答案,非常有帮助!已经投票了。@Akbar
猜你喜欢
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
相关资源
最近更新 更多