先把源码附上:ApiDemos->Text->Link.java
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.text;
import com.example.android.apis.R;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;
public class Link extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link);
// text1 shows the android:autoLink property, which
// automatically linkifies things like URLs and phone numbers
// found in the text. No java code is needed to make this
// work.
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
// text3 shows creating text with links from HTML in the Java
// code, rather than from a string resource. Note that for a
// fixed string, using a (localizable) resource as shown above
// is usually a better way to go; this example is intended to
// illustrate how you might display text that came from a
// dynamic source (eg, the network).
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
Html.fromHtml(
"<b>text3:</b> Text with a " +
"<a href=\"http://www.google.com\">link</a> " +
"created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());
// text4 illustrates constructing a styled string containing a
// link without using HTML at all. Again, for a fixed string
// you should probably be using a string resource, not a
// hardcoded value.
SpannableString ss = new SpannableString(
"text4: Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
}
}
再把布局代码附上:ApiDemos->res->layout->link.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Four TextView widgets, each one displaying text containing links. -->
<!-- text1 automatically linkifies things like URLs and phone numbers. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="all"
android:text="@string/link_text_auto"
/>
<!-- text2 uses a string resource containing explicit <a> tags to
specify links. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/link_text_manual"
/>
<!-- text3 builds the text in the Java code using HTML. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<!-- text4 builds the text in the Java code without using HTML. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
再把相关的字符资源贴上:ApiDemos->res->values->strings.xml
<string name="link_text_auto"><b>text1:</b> This is some text. In
this text are some things that are actionable. For instance,
you can click on http://www.google.com and it will launch the
web browser. You can click on google.com too. And, if you
click on (415) 555-1212 it should dial the phone.
</string>
<string name="link_text_manual"><b>text2:</b> This is some other
text, with a <a href="http://www.google.com">link</a> specified
via an <a> tag. Use a \"tel:\" URL
to <a href="tel:4155551212">dial a phone number</a>.
</string>
最后上效果图:
这里重点说一些讲解吧!
1.设置TextView支持Html内容:
TextView tv= (TextView) findViewById(R.id.text3);
tv.setText(Html.fromHtml("<b>text:</b> Text with a " +
"<a href=\"http://www.google.com\">link</a> " + created in the Java source code using HTML."));
2.设置TextView支持滚动:
tv.setMovementMethod(LinkMovementMethod.getInstance());
3.设置TextView支持点击拨打电话:
SpannableString ss = new SpannableString("text4: Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
..... 待续,go to 饭...
- 大小: 17.3 KB
分享到:
相关推荐
在Android开发中,ViewPager是一个非常重要的控件,它允许用户左右滑动来浏览多个页面,常用于实现类似轮播图、Tab切换等效果。这篇原创文章深入探讨了如何使用ViewPager,我们将从以下几个方面来理解这一知识点: ...
《Android移动开发一本就够了》是一本专为Android开发者编写的实战型教程,旨在帮助初学者或有一定基础的程序员深入理解Android应用开发的核心技术。源码.zip文件包含了这本书中提到的各种示例代码,是学习和实践...
- **基本组件使用**:Button、TextView等常用控件的使用方法。 - **生命周期理解**:Activity的生命周期,以及如何正确处理生命周期内的各种回调方法。 总之,这些期数的学习资料覆盖了从Android基础知识到高级应用...
android控件的抖动效果 很漂亮的ListView android 图像处理滤镜 照亮边缘特效 无闪烁启动画面 Android实现《天女散花》效果--(带源码) 天天动听 半透明Menu效果 Android 小項目之---Iphone拖动图片特效 (附源码) ...
- **Activity**:Activity是Android应用的四大组件之一,代表了一个屏幕上的交互界面。每个Activity都有自己的生命周期管理机制。 - **AndroidManifest.xml**:这是一个XML文件,用于声明应用的基本信息以及注册应用...
【Android 7.0 全套教程PPT 全部手写 原创】这一教程集合涵盖了Android开发的基础到高级知识,旨在帮助教师和初学者深入理解Android 7.0系统开发。作为一套原创的手写PPT,它强调了实践操作与理论知识的结合,以直观...
描述中提到,项目采用Java语言编写,这表明其遵循了Android应用开发的标准实践,因为Java是Android官方支持的主要编程语言之一。开发者提到“互相学习”,这意味着这个项目可能是开源的或者具有教学性质,供其他...
【安卓游戏2048源代码】是一款基于Android平台开发的简单数字拼合游戏,源自意大利开发者Gabriele Cirulli的原创作品1024。这款游戏的目标是通过上下左右滑动屏幕,使得屏幕上的数字方块不断合并,最终达到2048这个...
4. **UI设计**:通过XML布局文件创建用户界面,学习各种控件的使用,如Button、TextView、EditText等,以及布局管理器如LinearLayout、RelativeLayout和ConstraintLayout。 5. **事件处理**:讲解如何处理用户交互...
对于Android计算器的实现,首先需要理解Android的基本架构,包括四大组件(Activity、Service、Broadcast Receiver和Content Provider)、AndroidManifest.xml文件、布局文件(XML)以及各种UI控件的使用。...
在跟着B站Up主天哥在奔跑,学习Android的TextView控件时,想要实现一个跑马灯效果,但是按照视频中的代码发现文字是不会动的。视频中的代码如下: 然后经过弹幕的提醒,可能是跑马效果所需要的焦点被其他控件抢走...
在Android开发中,`EditText` 是一个非常常见的控件,用于接收用户输入的文字。然而,在实际应用中,我们经常需要对用户的输入进行一些限制,比如限制输入的字符数,或者不允许输入特定类型的表情符号。本篇文章将...
你提到的资源是一个名为"igridview-master"的压缩包,可能是某个开发者分享的九宫格布局的实现代码,可能基于Android平台,因为"GridView"是Android SDK中的一个控件,常用来实现类似的效果。 在Android开发中,`...