[英]TextView performClick() doesn't click a link
我有一个带有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();
}
}
只需在您的 xml 中使用此android:autoLink="web"
。 它对我有用。 让我知道它是否有效
使用autoLink="web"
。
<TextView
android:id="@+id/text"
android:text="abcdefgh@def.com"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web" />
要以编程方式执行此操作,请使用view.setMovementMethod(LinkMovementMethod.getInstance());
在您的代码中,并确保您的 XML 布局中没有android:autoLink="web"
。
示例:
TextView text = (TextView) findViewById(R.id.text);
text.setMovementMethod(LinkMovementMethod.getInstance());
编辑
也使用android:linksClickable="true"
。 它可能会起作用。 还要确保使用autoLink
或setMovementMethod
。 不要同时使用它们。
编辑 2
我意识到您想在performClick()
上使用performClick()
。 尽管android.widget.TextView.performClick()
例子并不多,这意味着该方法要么不受欢迎,要么过时。 但是它可以像这样实现:
TextView text ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.Text1);
text.setOnClickListener(this);
}
public void onClick(View arg0) {
Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
startActivity(i);
}
}
这符合您的要求。 检查它是否有效。
我们需要覆盖 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;
}
不幸的是,您试图实现的目标是不可能的。 基于 textView.performClick() 的文档:
调用此视图的 OnClickListener(如果已定义)
由于您尚未定义任何 onClickListener,这些方法将不起作用。
解决方法是定义您的自定义 onClickListener 并尝试以编程方式解析 URI 以触发适当的Intent.ACTION_VIEW
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.