我有一个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>
主要活动:
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();
}
}
解决方法:
我们需要重写performClick():
@Override
public boolean performClick() {
// Calls the super implementation, which generates an AccessibilityEvent
// and calls the onClick() listener on the view, if any
super.performClick();
// Handle the action for the custom click here
return true;
}