我们如何将TextView的链接打开到Webview中

时间:2011-08-31 09:47:23

标签: android

当我点击TextView的链接时,如何将WebView的链接打开到TextView

5 个答案:

答案 0 :(得分:28)

Spanned spanned = Html.fromHtml("<a href=\"http://google.com\">google.com</a>");
textView.setText(spanned);

编辑:这不是处理链接点击的理想方式,但我不知道其他任何方式。

您的主要活动包含一个带有链接的TextView。链接URL具有自定义方案。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView link = (TextView)findViewById(R.id.link);
        link.setText(
            Html.fromHtml("<a href='myscheme://www.google.com'>link</a>"));
        link.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

点击此链接后,Android会使用链接网址Activity启动ACTION_VIEW。假设您有WebViewActivity处理具有此自定义方案的URI。它获取传递的URI并用http替换其方案。

public class WebViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );

        if( savedInstanceState == null ) {
            String url =
                getIntent().getDataString().replace("myscheme://", "http://");
            // do something with this URL.
        }
    }
}

要处理自定义URI方案,WebViewActivity必须在AndroidManifest.xml文件中包含一个意图过滤器:

<activity android:name=".WebViewActivity" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="myscheme" />
    </intent-filter>
</activity>

答案 1 :(得分:1)

将属性 android:autolink 设置为textview布局

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:text="your text"
    android:autoLink="web" />

答案 2 :(得分:0)

以下为我工作。我刚刚使用了LinkMovementMethod的onTouchEvent代码。您可以像上面提到的Michael一样在textView中设置链接,也可以在strings.xml中设置它。我使用此库打开链接https://github.com/DreaminginCodeZH/CustomTabsHelper

textView.setMovementMethod(new MovementMethod() {
        @Override
        public void initialize(TextView widget, Spannable text) {

        }

        @Override
        public boolean onKeyDown(TextView widget, Spannable text, int keyCode, KeyEvent event) {
            return false;
        }

        @Override
        public boolean onKeyUp(TextView widget, Spannable text, int keyCode, KeyEvent event) {
            return false;
        }

        @Override
        public boolean onKeyOther(TextView view, Spannable text, KeyEvent event) {
            return false;
        }

        @Override
        public void onTakeFocus(TextView widget, Spannable text, int direction) {

        }

        @Override
        public boolean onTrackballEvent(TextView widget, Spannable text, MotionEvent event) {
            return false;
        }

        /**
         * Borrowed code for detecting and selecting link from
         * {@link LinkMovementMethod#onTouchEvent(TextView, Spannable, MotionEvent)}
         */
        @Override
        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
            int action = event.getAction();

            if (action == MotionEvent.ACTION_UP ||
                    action == MotionEvent.ACTION_DOWN) {
                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);

                ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

                if (link.length != 0) {
                    if (action == MotionEvent.ACTION_UP) {
                        // do whatever else you want here on link being clicked
                        Selection.removeSelection(buffer);

                    } else if (action == MotionEvent.ACTION_DOWN) {
                        Selection.setSelection(buffer,
                                buffer.getSpanStart(link[0]),
                                buffer.getSpanEnd(link[0]));
                    }

                    return true;
                } else {
                    Selection.removeSelection(buffer);
                }
            }

            return false;
        }

        @Override
        public boolean onGenericMotionEvent(TextView widget, Spannable text, MotionEvent event) {
            return false;
        }

        @Override
        public boolean canSelectArbitrarily() {
            return false;
        }
    });

答案 3 :(得分:0)

如果您复制nastylion的Kotlin解决方案,则可以像这样使用它:

textView.handleUrlClicks { url ->
    Timber.d("click on found span: $url")
    // Call WebView here.
}

您还可以看到an article及其LinkMovementMethodlinkify,但这太困难了(包含不同的遮罩和弹出菜单)。

答案 4 :(得分:-2)

hotel_website=(TextView)viewflipper.findViewById(R.id.tv_website);
hotel_website.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    String url = "http://" +hotel_website.getText().toString();
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
   }
});

和XML

<TextView
  android:clickable="true"
</TextView>