Android实现点击链接跳转功能

简介: Android实现点击链接跳转功能

实现效果图

点击用户协议到人工智能教程

点击隐私条款是到阿超的博客主页

(测试用例)

代码实现

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
    android:gravity="center"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@drawable/head_bg"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.8"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:gravity="center"
            android:layout_weight="1.5"
            android:orientation="vertical">

            <ImageView
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_gravity="center"
                android:src="@drawable/head_photo"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textStyle="bold"
                android:textSize="23sp" />
        </LinearLayout>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6"
        android:orientation="vertical">
        <FrameLayout
            android:id="@+id/login_fl"
            android:layout_width="300dp"
            android:layout_height="68dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="@drawable/login_button_bg">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="登录"
                android:textColor="#333333"
                android:textSize="18sp"
                android:clickable="false"/>
        </FrameLayout>

    </LinearLayout>
    <TextView
        android:id="@+id/loginPageTextView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"/>
</LinearLayout>

LoginActivity.java

package top.gjc;

public class LoginActivityextends BaseActivity {

    private TextView loginPageTextView; //文本框
    private Context myContext = LoginActivity.this;

    @Override
    protected int getResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        loginPageTextView= findViewById(R.id.loginPageTextView);

        findViewById(R.id.login_fl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //登录操作
            }
        });
    }

    @SuppressLint("ResourceAsColor") //忽略警告
    @Override
    protected void initData() {
        String string = "是阿超是阿超是阿超\n登录即代表您同意我们的";
        String str = "《用户\n协议》";
        String s = "《隐私条款》";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(string);
        SpannableString spannableStr = new SpannableString(str);
        SpannableString spannableS = new SpannableString(s);

        spannableStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
              // 点击用户协议(uri为跳转链接)
                Uri uri = Uri.parse("https://www.captainai.net/gjc");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }

            // 重写显示状态
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#6C6FFF"));
                ds.setUnderlineText(true);
            }
        }, 0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableS.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
              // 点击隐私条款(uri为跳转链接)
                Uri uri = Uri.parse("https://blog.csdn.net/Mr_Gaojinchao");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#6C6FFF"));
                ds.setUnderlineText(true);
            }
        }, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannableStringBuilder.append(spannableStr);
        spannableStringBuilder.append(" 和 ");
        spannableStringBuilder.append(spannableS);

        // 设置链接点击事件
        loginPageTextView.setMovementMethod(LinkMovementMethod.getInstance());
        // 设置点击文本时背景为透明状态(默认情况点击文本时 文本背景为淡蓝色)
        loginPageTextView.setHighlightColor(android.R.color.transparent);
        loginPageTextView.setText(spannableStringBuilder);
    }

}
目录
相关文章
|
5天前
|
XML Android开发 数据格式
android点击FrameLayout、LinearLayout等父布局没响应的原因以及解决方案
android点击FrameLayout、LinearLayout等父布局没响应的原因以及解决方案
42 2
|
5天前
|
Android开发
Android SystemUI去掉拖动亮度条QSPanel界面隐藏功能
Android SystemUI去掉拖动亮度条QSPanel界面隐藏功能
12 0
|
5天前
|
存储 应用服务中间件 网络安全
Android 网络链接稳定性测试解决方案
Android 网络链接稳定性测试解决方案
23 0
|
5天前
|
Java Android开发
Android Mediatek 应用层重置USB设备功能
Android Mediatek 应用层重置USB设备功能
14 0
|
5天前
|
Linux Android开发
Android 内核关闭CAN 串口设备回显功能
Android 内核关闭CAN 串口设备回显功能
13 0
|
5天前
|
机器学习/深度学习 人工智能 TensorFlow
安卓中的人工智能:集成机器学习功能
【4月更文挑战第14天】在数字化时代,人工智能与机器学习正驱动安卓平台的技术革新。谷歌的ML Kit和TensorFlow Lite为开发者提供了便捷的集成工具,使得应用能实现图像识别、文本转换等功能,提升用户体验。尽管面临数据隐私和安全性的挑战,但随着技术进步,更强大的AI功能将预示着移动端的未来,为开发者创造更多创新机遇。
|
5天前
|
Android开发
Android实现拨打电话功能
Android实现拨打电话功能
35 0
|
5天前
|
存储 数据库 Android开发
Android实现手机内存存储功能
Android实现手机内存存储功能
27 2
|
Android开发
Android back键及backWebview模式跳转详解
首先,来看一下关于Android home键和back键区别 back键 Android的程序无需刻意的去退出,当你一按下手机的back键的时候,系统会默认调用程序栈中最上层Activity的Destroy()方法来,销毁当前Activity。
1053 0
|
2天前
|
编解码 数据库 Android开发
安卓应用开发:打造高效用户界面的五大技巧
【5月更文挑战第18天】在竞争激烈的应用市场中,一个流畅且直观的用户界面(UI)对于安卓应用的成功至关重要。本文将探讨五种提升安卓应用用户界面性能的技巧,包括合理布局设计、优化资源使用、利用硬件加速、内存管理以及响应式编程。通过这些方法,开发者可以创建出既美观又高效的应用体验,从而吸引和保留用户。