嗨,我想从我的应用程序的 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"
。