Text In textView is not showing but the java code work. If i add autoLink="web" tag to textview , the text shows but java code doesn't

huangapple go评论67阅读模式
英文:

Text In textView is not showing but the java code work. If i add autoLink="web" tag to textview , the text shows but java code doesn't

问题

以下是翻译的部分:

文本视图中的文本未显示,但Java代码有效。如果我向TextView添加autoLink="web"标签,文本会显示,但Java代码不起作用。当按下TextView时,用户应该被重定向到联系电子邮件。

这是TextView的XML代码:

<TextView
    android:id="@+id/textViewContact"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_marginBottom="250dp"
    android:autoLink="email"
    android:fontFamily="@font/rubik_bold"
    android:textAlignment="center"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

如果我添加任何autoLink标签,TextView中的文本会显示,但用户不会被重定向到电子邮件。如果我删除标签,用户将被重定向到电子邮件,但TextView中的文本不会显示。

这是Java代码:

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.MailTo;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

public class ContactUs extends AppCompatActivity {

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

        TextView mail = (TextView) findViewById(R.id.textViewContact);
        mail.setText(Html.fromHtml("<a href=\"mailto:confidential@gmail.com\">Contact Us</a>"));
        mail.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

感谢您的提问!如有需要,请随时向我提出更多问题。

英文:

Text In textView is not showing but the java code work. If i add autoLink="web" tag to textview , the text shows but java code doesn't work. When the textview is pressed , user should be redirected to contact email.

This is the text view xml.

&lt;TextView
        android:id=&quot;@+id/textViewContact&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;75dp&quot;
        android:layout_marginBottom=&quot;250dp&quot;
        android:autoLink=&quot;email&quot;
        android:fontFamily=&quot;@font/rubik_bold&quot;
        android:textAlignment=&quot;center&quot;
        android:textColor=&quot;#FFFFFF&quot;
        android:textSize=&quot;30sp&quot;
        android:textStyle=&quot;bold&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

If i add any autoLink tag , the text in textview shows but the user is not redirected to email.
If i remove the tag , user is redirected to email but the textview text is not showing.

This is the java code:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.MailTo;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

public class ContactUs extends AppCompatActivity {

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

        TextView mail =(TextView) findViewById(R.id.textViewContact);
        mail.setText(Html.fromHtml(&quot;&lt;a href=\&quot;mailto:confidential@gmail.com\&quot;&gt;Contact Us&lt;/a&gt;&quot;));
        mail.setMovementMethod(LinkMovementMethod.getInstance());

    }
}

Thanks in advance for help!

答案1

得分: 1

你需要将以下代码添加到你的活动中,因为你正在使用的方法已经被弃用。

对于 Kotlin,使用以下代码片段:

if (Build.VERSION.SDK_INT >= 24) {
    mail.text = Html.fromHtml("<a href=\"mailto:confidential@gmail.com\">Contact Us</a>", Html.FROM_HTML_MODE_LEGACY)
} else {
    mail.text = Html.fromHtml("<a href=\"mailto:confidential@gmail.com\">Contact Us</a>")
}

对于 Java,使用以下代码:

if (Build.VERSION.SDK_INT >= 24) {
    mail.setText(Html.fromHtml("<a href=\"mailto:confidential@gmail.com\">Contact Us</a>", Html.FROM_HTML_MODE_LEGACY));
} else {
    mail.setText(Html.fromHtml("<a href=\"mailto:confidential@gmail.com\">Contact Us</a>"));
}

另外,在 TextView 标签中不需要设置 autolink 属性。你可以在 XML 中省略这行代码,这样你就能看到 "Contact Us" 文本,点击链接将会跳转到 Gmail。

Text In textView is not showing but the java code work. If i add autoLink="web" tag to textview , the text shows but java code doesn't

英文:

You need to add below code to your activity as the method you are using is deprecated

For kotlin, use below code snippet

 if (Build.VERSION.SDK_INT &gt;= 24) {
        mail.text = Html.fromHtml(&quot;&lt;a href=\&quot;mailto:confidential@gmail.com\&quot;&gt;Contact Us&lt;/a&gt;&quot;,Html.FROM_HTML_MODE_LEGACY)
    } else {
        mail.text = Html.fromHtml(&quot;&lt;a href=\&quot;mailto:confidential@gmail.com\&quot;&gt;Contact Us&lt;/a&gt;&quot;)
    }

For Java, Use below lines

if (Build.VERSION.SDK_INT &gt;= 24) {
        mail.setText(Html.fromHtml(&quot;&lt;a href=\&quot;mailto:confidential@gmail.com\&quot;&gt;Contact Us&lt;/a&gt;&quot;,Html.FROM_HTML_MODE_LEGACY));
    } else {
        mail.setText(Html.fromHtml(&quot;&lt;a href=\&quot;mailto:confidential@gmail.com\&quot;&gt;Contact Us&lt;/a&gt;&quot;));
}

Also there is no need of autolink property in the TextView tag. You can skip this line in xml and you will be able to see the Contact Us text and clicking on link will navigate you to gmail.
Text In textView is not showing but the java code work. If i add autoLink="web" tag to textview , the text shows but java code doesn't

huangapple
  • 本文由 发表于 2020年9月21日 16:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63988421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定