我有一个带有 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/