英文:
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.
<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" />
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("<a href=\"mailto:confidential@gmail.com\">Contact Us</a>"));
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。
英文:
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 >= 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>")
}
For Java, Use below lines
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>"));
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论