如何在android中实现链接,下面四种方式告诉你
2011-07-10 16:03
447 查看
绿色字体都是链接
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">kelly</string> <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> </resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="match_parent" android:autoLink="all" android:text="@string/link_text_auto" /> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/link_text_manual" /> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text3" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text4" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
mainactivity.java
package com.test.activity; import java.io.IOException; import android.app.Activity; import android.app.WallpaperManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.PorterDuff; import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; 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.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.ImageView.ScaleType; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // Be sure to call the super class. super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView t2 = (TextView) findViewById(R.id.text2); t2.setMovementMethod(LinkMovementMethod.getInstance()); 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()); 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()); } }
相关文章推荐
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)(源码 + Demo)
- Android四种存储方式 sharedpreference,file,SQlite,contentprovider实现增删改查
- Android自定义控件:进度条的四种实现方式
- Android四种存储方式 sharedpreference,file,SQlite,contentprovider实现增删改查
- Android:如何在ListView中嵌套ListView (如何实现二级目录结构?)的两种实现方式
- Android 按钮点击事件 四种实现方式
- 在一个Android项目里面有两个可运行文件,如何实现调用方式
- Android拨打电话的四种实现方式
- 详细解释如何通过Android自带的方式来实现图片的裁剪——原理分析+解决方案
- Android四种方式实现计时器
- Android 四种阴影实现方式对比
- Android点击事件的四种实现方式
- Android点击事件的四种实现方式
- Android四种存储方式 sharedpreference,file,SQlite,contentprovider实现增删改查
- Android四种存储方式 sharedpreference,file,SQlite,contentprovider实现增删改查
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)
- Android实现TextView中文字链接的4种方式介绍及代码
- android 事件响应的四种实现方式
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)(源码 + Demo)
- Android实现计算器布局(四种布局方式)之LinearLayout线性布局