gpt4 book ai didi

java - 如何 Intent (发送到其他 Activity )来自 textview android 的字符串的一部分?

转载 作者:搜寻专家 更新时间:2023-11-01 08:39:22 24 4
gpt4 key购买 nike

如何 Intent (发送到其他 Activity )来自 TextView 的字符串的一部分?像这张图片: intent part of string

我已经像这样在 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 - 如何 Intent (发送到其他 Activity )来自 textview android 的字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34263529/

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