【发布时间】:2018-04-05 11:24:03
【问题描述】:
我有一个TextView 和android:autoLink="all":
<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>
主活动:
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中设置TextView可点击
-
@Danger,谢谢,但没有帮助。