weixin_44640662 2022-07-02 15:55 采纳率: 72.7%
浏览 55
已结题

如何点击textview中部分文字实再跳转

我用android studio编了一个小的APP,界面上有textView5, textView7, button5, 现在想用代码增加以下功能。

  1. 点击button5,在textView5中显示:今天天气真好,其中“真好”两个字形成可以点击的链接。
  2. 点击textView5中“真好”两个字,在textView7中显示“成功实再跳转”

请帮我写一个实现上面功能的代码。(页面部署,按钮的监听等代码都不用,我的APP都可以正确使用。)
如果要用到特殊的类或包,请一并说明怎么弄。

不要讲原理,不要发一个链接给我,直接帮我写出代码,越简单越好。(如果你仅是发我一个其他地方代码的链接,就不要回答我这个问题了。谢谢!)

  • 写回答

1条回答 默认 最新

  • 铁拳小金刚 2022-07-04 09:02
    关注

    MainActivity的代码:

    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.graphics.Color;
    import android.os.Bundle;
    import android.text.Spannable;
    import android.text.SpannableStringBuilder;
    import android.text.method.LinkMovementMethod;
    import android.text.style.ClickableSpan;
    import android.text.style.ForegroundColorSpan;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    Button btn;
    TextView textView5,textView7;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    textView5.setText("今天天气真好");
                    final SpannableStringBuilder style = new SpannableStringBuilder();
    
                    //设置文字
                    style.append("今天天气真好");
                    ClickableSpan clickableSpan = new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            textView7.setText("成功实现再跳转");
                        }
                    };
                    style.setSpan(clickableSpan, 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textView5.setText(style);
    
                    //设置部分文字颜色
                    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0000FF"));
                    style.setSpan(foregroundColorSpan, 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
                    //配置给TextView
                    textView5.setMovementMethod(LinkMovementMethod.getInstance());
                    textView5.setText(style);
                }
            });
    
        }
        public void init(){
            btn=findViewById(R.id.button5);
            textView5=findViewById(R.id.textView5);
            textView7=findViewById(R.id.textView7);
        }
    }
    
    
    

    activity_main.xml的代码:

    
    ```<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="textView5"
            android:gravity="center"
            android:textSize="24dp"/>
        <TextView
            android:id="@+id/textView7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="textView7"
            android:gravity="center"
            android:textSize="24dp"/>
    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button5"
        />
    </LinearLayout>
    
    
    

    ```

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月12日
  • 已采纳回答 7月4日
  • 赞助了问题酬金10元 7月4日
  • 赞助了问题酬金10元 7月4日
  • 展开全部

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同