如何通过单击带有两个不同单词的文本视图来切换到另一个视图。这是我正在使用的字符串。
By clicking Sign Up, you are indicating that you have read and agree to the
Term of Use and Privacy Policy.
我想让这两个词(使用条款,隐私政策)显示不同的颜色并可点击。
我知道如何为特定单词设置颜色,但我也想让它们可以被点击。
我终于想出如何在TextView中实现多个可点击的部分。
重要的是,它们都必须有自己的ClickableSpan!这是我第一次测试时犯的错误。如果它们具有相同的ClickableSpan实例,则只有最后设置的span被记住。
我创建了一个由"["和"]"包围所需可点击区域的字符串。
String sentence = "this is [part 1] and [here another] and [another one]";
这里是设置TextView的部分,设置setMovementMethod也是必须的:
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(addClickablePart(sentence), BufferType.SPANNABLE);
我已经创建了这个函数,它将处理可点击区域的创建:private SpannableStringBuilder addClickablePart(String str) {
SpannableStringBuilder ssb = new SpannableStringBuilder(str);
int idx1 = str.indexOf("[");
int idx2 = 0;
while (idx1 != -1) {
idx2 = str.indexOf("]", idx1) + 1;
final String clickString = str.substring(idx1, idx2);
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(getView().getContext(), clickString,
Toast.LENGTH_SHORT).show();
}
}, idx1, idx2, 0);
idx1 = str.indexOf("[", idx2);
}
return ssb;
}
sentence
字符串有 [
和 ]
,它们不应该包含在显示的字符串中吗? - Compaq LE2202xssb
,而不是在外部仅初始化一次? - Boy根据Boy的回复(感谢您的回复,帮了我很多),这是我实现它的另一种方式,使用内部类来描述可点击的单词,而不使用'['和']'字符:
import java.util.List;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Defines a TextView widget where user can click on different words to see different actions
*
*/
public class ClickableTextView extends TextView {
public ClickableTextView(Context context) {
super(context);
}
public ClickableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClickableTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setTextWithClickableWords(String text, List<ClickableWord> clickableWords) {
setMovementMethod(LinkMovementMethod.getInstance());
setText(addClickablePart(text, clickableWords), BufferType.SPANNABLE);
}
private SpannableStringBuilder addClickablePart(String str, List<ClickableWord> clickableWords) {
SpannableStringBuilder ssb = new SpannableStringBuilder(str);
for (ClickableWord clickableWord : clickableWords) {
int idx1 = str.indexOf(clickableWord.getWord());
int idx2 = 0;
while (idx1 != -1) {
idx2 = idx1 + clickableWord.getWord().length();
ssb.setSpan(clickableWord.getClickableSpan(), idx1, idx2, 0);
idx1 = str.indexOf(clickableWord.getWord(), idx2);
}
}
return ssb;
}
public static class ClickableWord {
private String word;
private ClickableSpan clickableSpan;
public ClickableWord(String word, ClickableSpan clickableSpan) {
this.word = word;
this.clickableSpan = clickableSpan;
}
/**
* @return the word
*/
public String getWord() {
return word;
}
/**
* @return the clickableSpan
*/
public ClickableSpan getClickableSpan() {
return clickableSpan;
}
}
}
希望这可以帮助到某些人
编辑:如何更改链接颜色并去掉下划线:
创建并使用自己的ClickableSpan实现,如下所示:
//a version of ClickableSpan without the underline
public static class NoUnderlineClickableSpan extends ClickableSpan {
private int color = -1;
public void setColor(int color) {
this.color = color;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
if (this.color != -1) {
ds.setColor(this.color);
}
}
}
在TextView中使用HTML链接比创建多个TextView、处理布局、监听器等要简单得多。
在您的Activity或Fragment中:
TextView mText = (TextView) findViewById(R.id.text_linkified);
mText.setText(Html.fromHtml("Open <a href='myapp://my-activity'>My Activity</a> or" +
" <a href='myapp://other-activity'>Other Activity</a>"));
mText.setMovementMethod(LinkMovementMethod.getInstance());
在清单文件中
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="myapp" android:host="my-activity"/>
</intent-filter>
</activity>
<activity android:name=".MyOtherActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="myapp" android:host="other-activity" />
</intent-filter>
</activity>
@Override public void onClick(View widget) { // 如何移除这个“widget”,并刷新edittext。 }
}, idx1, idx2, 0); - Sakthivel Appavu