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日
  • 展开全部

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料