Я хочу, чтобы текстовое представление было доступно для кликов

<TextView
    android:id="@+id/link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Forget password?"
    android:autoLink="all"
    android:linksClickable="true"
    android:textColor="@color/lgreen"
    android:textStyle="italic"
     />

Android выделяет ссылки в TextView, но они не реагируют на клики. Может кто-то сказать мне, как я могу сделать это как кликабель и перейти к другой ссылке. Я попробовал больше раз, увидев примеры. но я не могу. Может ли кто-нибудь объяснить мне, как изменить текстовое представление как ссылку на клики.

Ответы

Ответ 1

Все проверенные и работающие 100%
ниже - полный пример
Решение: android: autoLink = "web"
Пример Xml

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="email"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/lostpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="web"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/defaultpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

Строка в string.xml

<string name="lostpassword">If you lost your password please contact <a href="mailto:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>

Ответ 2

Вы можете добавить прослушиватель кликов в TextView и запустить websearch:

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view)
    {
         Intent browser= new Intent(Intent.ACTION_VIEW, Uri.parse(PASTE_YOUR_URL_HERE));  
         startActivity(browser);  
    }

});

и xml выглядит следующим образом:

<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/link_string"  
    android:textSize="14dp"  
    android:autoLink="web" /> 

Ответ 3

В строковый файл поместите этот код

<string name="link">'<a href="http://www.www.com" >ForGet Password</a>'</string> 

и в файле XML

android:text="@string/link"

Ответ 4

это ответ EDITED

Привет, вы можете попробовать, заменив это в файле layout.xml

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="visit the site at www.google.com from here"
    android:autoLink="all"
    android:clickable="true"
    android:linksClickable="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Я попробовал, и это наверняка сработает. и рассматривает "www.google.com" как веб-ссылку

Ответ 5

это самый простой и понятный код

TextView link = (TextView) findViewById(R.id.hyper);
String linkText = "This is my website <a href='http://programondaspot.blogspot.com'>Programondaspot</a> .";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());

Посмотрите post!

СЧАСТЛИВЫЙ КОДИРОВАНИЕ!

Ответ 6

В вашем методе onCreate() введите этот код:

TextView clickableTextLink = (TextView)findViewById(R.id.your_textview_id); clickableTextLink.setMovementMethod(LinkMovementMethod.getInstance());

Я попробовал ряд решений. Но это отлично сработало для меня.

Ответ 7

Просто добавьте тег onClick в свой TextView, который равен определенной функции. Как показано ниже

<TextView
  android:id="@+id/link"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:text="Forget password?"
  android:autoLink="all"
  android:linksClickable="true"
  android:textColor="@color/lgreen"
  android:textStyle="italic"
  android:onClick="callFunction"/>

Ответ 8

Вот простая версия, протестированная до Android N

    String termsText = "By registering, you are agree to our";
    String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
    String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
    String allText = termsText + termsLink + privacyLink;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
        ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
    } else {
        ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
        ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
    }