如何从 TextView 中 Intent (发送到其他 Activity )字符串的一部分?就像这张图片:
我已经在 textview 上设置了这样的文本:
lbl_checkback_tommorow = (TextView) findViewById(R.id.lbl_checkback_tommorow);
final String update_profile = "update your profile";
final String hot_news = "Hot news?";
lbl_checkback_tommorow.setText("Check back tommorow,\n and you should be ready to start booking your workout. While you wait, would you want to "+update_profile+", or read some of the interesting posts we have in our "+hot_news);
如何使 update_profile 和 hot_news 可点击(转到另一个 Activity )?
更新: 可点击的跨度如下:
SpannableString update_profile = new SpannableString("update your profile");
SpannableString hot_news = new SpannableString("Hot news?");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
startActivity(new Intent(MyActivity.this, NextActivity.class));
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
update_profile.setSpan(clickableSpan, 118, 215, 234, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
hot_news.setSpan(clickableSpan, 118, 215, 234, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableStringBuilder builder = new StringBuilder("Check back tommorow,\n and you should be ready to start booking your workout. While you wait, would you want to "+update_profile+", or read some of the interesting posts we have in our "+hot_news);
lbl_checkback_tommorow = (TextView) findViewById(R.id.lbl_checkback_tommorow);
lbl_checkback_tommorow.setText(builder);
最佳答案
How to make update_profile and hot_news clickable (go to another activity)?
使用 SpannableStringBuilder
您可以将文本转换为 Spannable 对象并设置 ClickableSpan在您想要使其可点击的文本部分上。当 ClickableSpan 的 onClick 被调用时,您可以启动 Activity。
编辑
这样就可以了
final String part1 = "Check back tommorow, and you should be ready to start booking your workout. While you wait, would you want to ";
final String part2 = " , or read some of the interesting posts we have in our ";
SpannableStringBuilder builder = new SpannableStringBuilder(part1);
SpannableString update_profile = new SpannableString("update your profile");
SpannableString hot_news = new SpannableString("Hot news?");
update_profile.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "first", Toast.LENGTH_LONG).show();
}
}, 0, update_profile.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(update_profile);
builder.append(part2);
hot_news.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "second", Toast.LENGTH_LONG).show();
}
}, 0, hot_news.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(hot_news);
lbl_checkback_tommorow.setText(builder);
lbl_checkback_tommorow.setMovementMethod(LinkMovementMethod.getInstance());
关于java - 如何从 android TextView 中 Intent (发送到其他 Activity )字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34263529/