Открыть гиперссылки Textview в WebView Android - PullRequest
0 голосов
/ 26 мая 2020

Ниже приводится текст, который я должен установить в текстовом представлении. Я хотел открыть веб-просмотр по щелчку гиперссылки. Остальной текст не должен быть интерактивным.

String value = "Check on this link: <a href="http://www.google.com">Go to Google</a>";                                
 binding.text.setText(value);

 <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:autoLink="web"
    android:textColorLink="@color/g_turquoise_blue" />

Заранее спасибо.

1 Ответ

0 голосов
/ 26 мая 2020

Это поможет вам, если я правильно понял

yourTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = "https://google.com";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });

ОБНОВЛЕНИЕ: Попробуйте это

/**
 * Returns a list with all links contained in the input
 */
public static List<String> extractUrls(String text)
{
    List<String> containedUrls = new ArrayList<String>();
    String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
    Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
    Matcher urlMatcher = pattern.matcher(text);

    while (urlMatcher.find())
    {
        containedUrls.add(text.substring(urlMatcher.start(0),
                urlMatcher.end(0)));
    }

    return containedUrls;
}

Пример:

List<String> extractedUrls = extractUrls("Welcome to https://stackoverflow.com/ and here is another link http://www.google.com/ \n which is a great search engine");

for (String url : extractedUrls)
{
    System.out.println(url);
}

Печатает:

https://stackoverflow.com/
http://www.google.com/

источник: Определить и извлечь URL-адрес из строки?

...