实现效果图
点击用户协议到人工智能教程
点击隐私条款是到阿超的博客主页
(测试用例)
代码实现
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); } }