简体   繁体   English

TextView performClick() 不点击链接

[英]TextView performClick() doesn't click a link

I have a TextView with android:autoLink="all" :我有一个带有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" />

For some reason I want to open a link set in android:text (it may be phone number, email, etc.).出于某种原因,我想打开一个在android:text设置的链接(可能是电话号码、电子邮件等)。 When I run an application, a performClick() doesn't open the link.当我运行应用程序时, performClick()不会打开链接。

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

Linkify.addLinks(buffer, Linkify.ALL); and some others don't help.而其他一些则无济于事。

UPDATE更新

Thank you for replies.感谢您的回复。 Show my code after receiving 3 answers.收到 3 个答案后显示我的代码。 I use API 19, 25 emulators and device (API 21).我使用 API 19、25 模拟器和设备 (API 21)。 Sadly, nothing works.可悲的是,没有任何作用。

<?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>

MainActivity:主要活动:

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();
    }
}

Just use this android:autoLink="web" in your xml and nothing else.只需在您的 xml 中使用此android:autoLink="web" It worked for me.它对我有用。 And let me know if it works让我知道它是否有效

Use 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" />

To do it programmatically, use view.setMovementMethod(LinkMovementMethod.getInstance());要以编程方式执行此操作,请使用view.setMovementMethod(LinkMovementMethod.getInstance()); in your code and make sure you do not have android:autoLink="web" in your XML layout.在您的代码中,并确保您的 XML 布局中没有android:autoLink="web"

Example:示例:

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

EDIT编辑

Use android:linksClickable="true" also.也使用android:linksClickable="true" It might work.它可能会起作用。 Also make sure to use either autoLink or setMovementMethod .还要确保使用autoLinksetMovementMethod Do not use both of them together.不要同时使用它们。

EDIT 2编辑 2

I realise you want to use performClick() on your textView.我意识到您想在performClick()上使用performClick() Although there aren't many examples for android.widget.TextView.performClick() , which means the method is either unpopular or old.尽管android.widget.TextView.performClick()例子并不多,这意味着该方法要么不受欢迎,要么过时。 However it can be implemented like this:但是它可以像这样实现:

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);
    }
}

This matches with your requirement.这符合您的要求。 Check if it works.检查它是否有效。

we need to override 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;
 }

Unfortunately, the thing you are trying to achieve is not possible.不幸的是,您试图实现的目标是不可能的。 Based on documentation of textView.performClick():基于 textView.performClick() 的文档:

Call this view's OnClickListener, if it is defined调用此视图的 OnClickListener(如果已定义)

Since you haven't defined any onClickListener, the approaches won't work.由于您尚未定义任何 onClickListener,这些方法将不起作用。

The workaround would be to define your custom onClickListener and try to parse the URI programatically to trigger appropriate Intent.ACTION_VIEW解决方法是定义您的自定义 onClickListener 并尝试以编程方式解析 URI 以触发适当的Intent.ACTION_VIEW

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM