java - 如何从 android TextView 中 Intent (发送到其他 Activity )字符串的一部分?

标签 java android android-intent

如何从 TextView 中 Intent (发送到其他 Activity )字符串的一部分?就像这张图片: 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 - 如何从 android TextView 中 Intent (发送到其他 Activity )字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34263529/

相关文章:

Android Intent 不会从 Switch 传递值

java - 有一个 char 引用数组中的对象

Java/Swing : Why is my JFileChooser opening twice?

java - 无法使用 Intent.ACTION_SEND_MULTIPLE 找到工作解决方案以将多个图像附加到电子邮件中

android - 如何在操作栏的多个菜单项中同时显示图标和文本?

android - 如何通过单击按钮打开 google play 商店

android - 将整个 ArrayList<String> 从一个 Activity 传递到 android 中的另一个 Activity

java - 通过 streamreader 读取多个 java 属性

java - 为什么我的同步代码块似乎忽略了同一对象上另一个正在等待的同步块(synchronized block)?

android - 如何将 Android 库导出为 .aar 并保留我的 Gradle 依赖项?