gpt4 book ai didi

java - 将 TextView 更改为自定义 ListView 中的可点击单词

转载 作者:行者123 更新时间:2023-12-01 18:02:57 25 4
gpt4 key购买 nike

我有一个自定义列表,其中每个列表上都有不同的按钮。我的目标是制作单个 TextView

    <TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Example"
android:textSize="16dp"
android:layout_below="@+id/rl1" />

根据句子进行更改,以便每个单词都可以点击来自 enter image description here
至此enter image description here
并将Word传递给函数。

我在想,如果我们可以将单个 TextView 划分为多个 TextView 并使它们可点击,这可能是非 XML 解决方案吗?
任何 fragment 都会感激
问候

最佳答案

请尝试此代码,因为您需要 Spannable 字符串:

     SpannableString ss = new SpannableString("Hello World");
ClickableSpan span1 = new ClickableSpan() {
@Override
public void onClick(View textView) {
// do first word click work
}
};

ClickableSpan span2 = new ClickableSpan() {
@Override
public void onClick(View textView) {
// do second word click work
}
};

ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());

谢谢

关于java - 将 TextView 更改为自定义 ListView 中的可点击单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39249213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com