您好,我想在我的应用的TextView中建立链接,同时在我的应用内打开WebView Activity
。
我可以使用扩展LinkMovementMethod
类的自定义类来处理链接点击。以下是代码:
public class CustomLinkMovementMethod extends LinkMovementMethod {
private static Context movementContext;
private static CustomLinkMovementMethod linkMovementMethod = new CustomLinkMovementMethod();
public boolean onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, android.view.MotionEvent event)
{
int action = event.getAction();
if (action == MotionEvent.ACTION_UP)
{
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length != 0)
{
String url = link[0].getURL();
if (url.contains("https"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "https Link was clicked", Toast.LENGTH_LONG).show();
} else if (url.contains("tel"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "Tel was clicked", Toast.LENGTH_LONG).show();
} else if (url.contains("mailto"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "Mail link was clicked", Toast.LENGTH_LONG).show();
} else if (url.contains("http"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "http Link was clicked", Toast.LENGTH_LONG).show();
}else if (url.contains("www"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "www Link was clicked", Toast.LENGTH_LONG).show();
}
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
public static android.text.method.MovementMethod getInstance(Context c){
movementContext = c;
return linkMovementMethod;
}
}
这是我的MainActivity的布局
<RelativeLayout 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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/textView"
android:linksClickable="true"
android:autoLink="all"
android:text="@string/hello_world" />
</RelativeLayout>
这是我的MainActivity类
public class MainActivity extends Activity {
TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
textView.setMovementMethod(CustomLinkMovementMethod.getInstance(MainActivity.this));
//textView.setMovementMethod(LinkMovementMethod.getInstance());
String customHtml2 ="<div>not valid => <a href='http://facebook.com'>http://facebook</a></div>" +
" <div><a href='http://www.facebook.com'>http://www.facebook.com</a></div>" +
" <div>http://www.google.com <= a regular link<div>" +
" <div>www.google.com <= a regular link<div>" +
" <div><a href='http://google.com'>http://google.com</a></div>" +
" <div><strong style='font-family:Arial, Verdana;font-weight:normal;'>http://www.google.com</strong></div>" +
" <div><strong style='font-family:Arial, Verdana;font-weight:normal;'></strong></div>" +
"";
textView.setText(Html.fromHtml(customHtml2));
}
}
问题是textview下面的属性使得不要触发CustomLinkMovement类中的onTouchEvent方法并打开默认的Web浏览器。如果我删除它们,则此http://www.google.com之类的链接不可点击,因为它未包含在内 标签。
android:linksClickable="true"
android:autoLink="all"
问题是我希望它们可以点击,即使链接没有包含在标签中并且能够同时使用android:linksClickable="true" and android:autoLink="all"
。
答案 0 :(得分:2)
使用
Linkify.addLinks(yourTextView, Linkify.WEB_URLS);
唯一的限制是您的地址应以http://
答案 1 :(得分:-1)