Java 如何从textview android中指定(发送到其他活动)字符串的一部分?

Java 如何从textview android中指定(发送到其他活动)字符串的一部分?,java,android,android-intent,Java,Android,Android Intent,如何从textview中指定(发送到其他活动)字符串的一部分?如下图所示: 我已经在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_tommor

如何从textview中指定(发送到其他活动)字符串的一部分?如下图所示:

我已经在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);
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);
如何使更新个人资料和热门新闻可点击(转到其他活动)

更新: 如下所示的可点击范围:

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);
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);
如何使更新个人资料和热门新闻可点击(转到其他网站 活动)

使用
SpannableStringBuilder
可以将文本转换为可单击对象,并在文本中要使其可单击的部分设置。调用ClickableSpan的onClick时,可以启动活动

编辑

这就行了

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

创建多个文本视图并随后附加是的,创建多个文本视图和只创建两个文本视图比较容易,但是如何只使用字符串来实现,就像我的问题一样?或多或少。您需要实例化一个提供文本的
SpannableStringBuilder
(SpannableStringBuilder=new-StringBuilder(“回过头来检查,\n您应该……)“`,然后您必须查看update_profile和hot_news的开始索引和结束索引,并使用这些索引调用builder对象上的setSpan。之后,您必须将builder设置为TextView的文本setSpan是什么,它是用来给字符串着色的?就像html中的span一样。我使用SpannableStringBuilder和settext更新代码作为生成器。对吗?
setSpan
将可扩展对象应用于生成器。在这种情况下,不能使用
+
连接字符串。请尝试使用
builder.append(热门新闻)