• Android实现点击链接跳转功能


    实现效果图

    点击用户协议到人工智能教程
    PS:前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,在这里分享给大家,一起学习,感兴趣的朋友可以进去看一看:点击即可进入人工智能教程
    点击隐私条款是到阿超的博客主页
    (测试用例)
    在这里插入图片描述

    代码实现

    activity_login.xml

    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    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);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    弘辽科技:淘宝店铺信用等级怎么看?信用等级怎么提升?
    高能预警!第十七届 D2 第一波话题新鲜出炉 ~
    卷积神经网络的实际应用,卷积神经网络毕业论文
    element-plus table组件单击行切换选中状态、点击高亮行、设置shift或ctrl连续多选和连续取消多选(支持多段选择)
    Go基础3:函数、结构体、方法、接口
    569. 员工薪水中位数
    微信小程序上传文件或图片(包含base64)至七牛云
    学习笔记——交通安全分析13
    SASE , sdp等
    BPA、BPM、BPR傻傻分不清楚?与RPA又有何关系?
  • 原文地址:https://blog.csdn.net/Mr_Gaojinchao/article/details/126198261