处理textview Android中的多个链接以在WebView活动而不是浏览器中打开?

时间:2015-05-06 06:02:19

标签: android webview textview

我的textview有时会被多个链接填充,有时会链接到单个链接。当用户点击一个链接时,它应该在具有webview的Activity中完全打开相同的链接。

String link =“http://www.google.com \”> http://www.google.com“; String link1 =“http://www.google.com”> http://www.google.com http://www.facebook.com \“> http://www.facebook.com”;

我使用以下参考来实现自定义链接移动方法,但它仍然在浏览器中打开。

enter link description here

但它仍会在浏览器中打开:

以下是我的代码:

MainActivity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.testtv);

        textView.setText(Html
                .fromHtml("<a href=\"http://www.google.com\">http://www.google.com</a>"));
        textView.setMovementMethod(CustomLinkMovementMethod.getInstance());

    }

主要活动xml:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.textviewtest.MainActivity" >

    <TextView
        android:id="@+id/testtv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textAppearance="@android:style/TextAppearance.Small" />

</RelativeLayout>

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textviewtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".WebViewActivity"
            android:label="@string/title_activity_web_view" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>
    </application>

</manifest>

CustomLinkMovementMethod类,取自上面的参考:

package com.example.textviewtest;

import android.content.Context;
import android.content.Intent;
import android.text.Layout;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

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.startsWith("https") || url.startsWith("http")) {
                    Log.d("Link", url);
                    Toast.makeText(movementContext, "Link was clicked",
                            Toast.LENGTH_LONG).show();

                    movementContext.startActivity(new Intent(movementContext,
                            WebViewActivity.class));
                } else if (url.startsWith("tel")) {
                    Log.d("Link", url);
                    Toast.makeText(movementContext, "Tel was clicked",
                            Toast.LENGTH_LONG).show();
                } else if (url.startsWith("mailto")) {
                    Log.d("Link", url);
                    Toast.makeText(movementContext, "Mail 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;
    }
}

我想处理此问题,并在使用webview的活动中打开textview中打开的特定链接?

0 个答案:

没有答案