android - TextView performClick() 不点击链接

标签 android textview

我有一个带有 android:autoLink="all"TextView:

<TextView
    android:id="@+id/text"
    android:text="abcdefgh@def.com"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="all" />

出于某种原因我想打开一个在android:text中设置的链接(它可能是电话号码,电子邮件等)。当我运行应用程序时,performClick() 没有打开链接。

TextView textView = findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.performClick();

Linkify.addLinks(buffer, Linkify.ALL); 和其他一些没有帮助。

更新

感谢您的回复。收到 3 个答案后显示我的代码。我使用 API 19、25 模拟器和设备 (API 21)。可悲的是,没有任何效果。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:text="abcdefgh@def.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:autoLink="web|email|phone" />

</android.support.constraint.ConstraintLayout>

主要 Activity :

public class MainActivity extends AppCompatActivity {

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

        TextView text = findViewById(R.id.text);
        int mask = Linkify.ALL;
        Linkify.addLinks(text, mask);
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.performClick();
    }
}

最佳答案

只需在您的 xml 中使用此 android:autoLink="web" 即可。它对我有用。让我知道它是否有效

关于android - TextView performClick() 不点击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49671166/

相关文章:

java - 无法在 android 中将 Drawable 转换为 LayerDrawable

android - 如何将监听器附加到 ListView ?

android - 我如何在 android 上使用相机权限解决此错误?

Android Studio Gradle 不使用传递依赖项

禁用时Android TextView 颜色不会改变

android - 如何在 4.2 之前的 Android 版本上处理 RTL 语言?

java - 使用layout_width ="wrap_content"获取TextView的可用宽度

android - 在Android中创建图像类型TextView

java - 如果我有下一个结构,如何从 Firebase 实时数据库检索所有数据?

android - 如何使用 WebView 在新窗口中启动 Activity ?