为了账号安全,请及时绑定邮箱和手机立即绑定

将链接更改为可在 TextView 上单击的自定义字符串

将链接更改为可在 TextView 上单击的自定义字符串

jeck猫 2024-01-05 10:01:42
我有链接https://google.com。我希望该链接只需更改为字符串“google”TextView 中显示的是“google”但实际上在谷歌有链接https://google.com。并且可以点击我有 XML:<TextViewandroid:id="@+id/social"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="30dp"android:layout_marginTop="350dp"android:autoLink="web"android:linksClickable="true"android:text="in here social"/>我有Java课程: TextView social = (TextView) findViewById(R.id.social); String text = "<a href='https://google.com'> Google </a>"; social.setText(Html.fromHtml(text));
查看完整描述

3 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

尝试这个:


 TextView t2 = (TextView) findViewById(R.id.text2);

t2.setMovementMethod(LinkMovementMethod.getInstance());

在 xml 中:


<TextView

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/txtCredits"/>

如果您正在调用,请在 xml 中删除自动链接setMovementMethod()。


查看完整回答
反对 回复 2024-01-05
?
动漫人物

TA贡献1815条经验 获得超10个赞

您可以将网址保存在标签中,如下所示:

social.setTag("https://google.com");

当你想获取 url 时,请执行以下操作:

String url = (String) social.getTag();


查看完整回答
反对 回复 2024-01-05
?
大话西游666

TA贡献1817条经验 获得超14个赞

Alhamdulillah 我的问题已经解决了


在你的XML中:


<TextView

android:id="@+id/social"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="30dp"

android:layout_marginTop="350dp"

android:linksClickable="true"

android:text="@string/social"/>

在你的价值观字符串中


<string name="social">in here social</string>

在你的java类中


TextView mSocial = (TextView) findViewById(R.id.social);

mSocial.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_VIEW);

                intent.setData(Uri.parse(https://google.com));

                startActivity(intent);

            }

        });

将您的 TextView 设置为字符串“Social”,但如果单击 TextView 可以转到 google.com


查看完整回答
反对 回复 2024-01-05
  • 3 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信