简体   繁体   中英

TextView performClick() doesn't click a link

I have a TextView with 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" />

For some reason I want to open a link set in android:text (it may be phone number, email, etc.). When I run an application, a performClick() doesn't open the link.

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. I use API 19, 25 emulators and device (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. It worked for me. And let me know if it works

Use 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()); in your code and make sure you do not have android:autoLink="web" in your XML layout.

Example:

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

EDIT

Use android:linksClickable="true" also. It might work. Also make sure to use either autoLink or setMovementMethod . Do not use both of them together.

EDIT 2

I realise you want to use performClick() on your textView. Although there aren't many examples for android.widget.TextView.performClick() , which means the method is either unpopular or old. 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():

@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():

Call this view's OnClickListener, if it is defined

Since you haven't defined any onClickListener, the approaches won't work.

The workaround would be to define your custom onClickListener and try to parse the URI programatically to trigger appropriate Intent.ACTION_VIEW

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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