如何在不使用Java的情况下在Android上的TextView中制作可点击的网址链接

Jig*_*tel 3 android android-layout android-xml

在这里,我使用了可点击的URL Textview,它可以工作。但是,如何使用浏览器通过打开的URL设置可点击的并突出显示的文本。可以使用Android XML或kotlin,而无需使用Java代码setText(Html.fromHtml(""))

String value = "<html>Visit Web <a href=\"http://www.domainname.com\">mysite</a> View</html>";
    TextView text = (TextView) findViewById(R.id.text);
    text.setText(Html.fromHtml(value));
    text.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)

Jig*_*tel 6

在此处输入图片说明

<TextView
        android:textSize="18sp"
        android:autoLink="web"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="http://www.example.com"
        tools:ignore="HardcodedText" />
Run Code Online (Sandbox Code Playgroud)

不需要明确的布局ID,也不需要Java端的代码。它是从XML工程

使用自动链接

    • 对于网站 android:autoLink="web"
    • 致电 android:autoLink="phone"
    • 对于电子邮件 android:autoLink="email"
    • 对于地图 android:autoLink="web"
    • 对所有人 android:autoLink="all"


小智 5

此属性适用于您的TextView:android:autoLink =“ web”

这是一个示例Layout layout.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/txtDefaultpassword"
        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" />
Run Code Online (Sandbox Code Playgroud)

string.xml

<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</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>
Run Code Online (Sandbox Code Playgroud)


Man*_*ish 5

只需将此属性添加到您的 TextViewandroid:autoLink="web" 例如

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:autoLink="web"
        android:clickable="true"
        android:text="https://www.google.co.in"/>
Run Code Online (Sandbox Code Playgroud)