1、运行效果图:

当点击图中的红色字体时,会跳转到相应的界面
2、xml布局文件相关代码:
复制代码
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<TextView android:id="@+id/agree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginTop="15dp" android:text="注册即代表已阅读并同意使用条款和隐私政策" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp"> <TextView android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已有账号,前去登录" /> <Button android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_marginLeft="10dp" android:background="@drawable/user_or_privacy" android:textSize="18dp" /> </LinearLayout>
3、Activity相关代码:
复制代码
1
2TextView agree = (TextView) findViewById(R.id.agree);//找到相应的控件 TextView login = (TextView) findViewById(R.id.login);
复制代码
1
2
3
4
5
6//借助SpannableString类实现超链接文字 agree.setText(getClickableSpan()); login .setText(getClickableSpan2()); //设置超链接可点击 agree.setMovementMethod(LinkMovementMethod.getInstance()); login.setMovementMethod(LinkMovementMethod.getInstance());
复制代码
总的来说思路很简单,其实就是,在一段话中截取一部分需要点击的字,设置上颜色和下划线,再设置上点击事件进行跳转就可以了。。
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
45private SpannableString getClickableSpan() { SpannableString spannableString = new SpannableString("注册即代表已阅读并同意使用条款和隐私政策"); //设置下划线文字 spannableString.setSpan(new UnderlineSpan(), 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置文字的单击事件 spannableString.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { // Toast.makeText(MainActivity.this,"使用条款",Toast.LENGTH_LONG).show(); Intent intent=new Intent(RegisterActivity.this,UserOrPrivacy.class); intent.putExtra("data","use"); startActivity(intent); } }, 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置文字的前景色 spannableString.setSpan(new ForegroundColorSpan(Color.RED), 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置下划线文字 spannableString.setSpan(new UnderlineSpan(), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置文字的单击事件 spannableString.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { // Toast.makeText(MainActivity.this,"隐私政策",Toast.LENGTH_LONG).show(); Intent intent=new Intent(RegisterActivity.this,UserOrPrivacy.class); intent.putExtra("data","privacy"); startActivity(intent); } }, 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new ForegroundColorSpan(Color.RED), 16, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString; } private SpannableString getClickableSpan2(){ SpannableString spannableString2 = new SpannableString("已有账号,前去登录"); spannableString2.setSpan(new UnderlineSpan(), 7, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString2.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Intent intent=new Intent(RegisterActivity.this,LoginActivity.class); intent.putExtra("name",name); startActivity(intent); } }, 7, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString2.setSpan(new ForegroundColorSpan(Color.RED), 7, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableString2; }
最后
以上就是诚心犀牛最近收集整理的关于Android探索之路:在界面上实现超链接进行跳转的全部内容,更多相关Android探索之路内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复