您好,我想从我的应用程序的 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 方法不会被触发并打开默认的网络浏览器。如果我删除它们这样的链接 http://www.google.com不可点击,因为它没有包含在 标签。
android:linksClickable="true"
android:autoLink="all"
问题是即使链接没有包含在标签中,我也希望它们是可点击的,并且能够在同时。
最佳答案
使用
Linkify.addLinks(yourTextView, Linkify.WEB_URLS);
唯一的限制是您的地址应以 http://
关于android - 使 TextView 中的链接在 Android 应用程序中打开 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108888/