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 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗