简体   繁体   中英

OnClickListener for TextView does not get called

I have a couple of text links in my Android applications which are either email addresses or web links . I use the following code snippet to set them up (example for a web link):

TextView textView = (TextView) findViewById(textViewId);
spannedText = Html.fromHtml("<a href=\"" + url + "\">" + titleText + "</a>");
textView.setText(spannedText, TextView.BufferType.SPANNABLE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Handle click
    }
});

The TextView has no special attributes set such as android:linksClickable , android:clickable or android:focusable . Its style inherits from @android:style/TextAppearance.Small .

For some reason the OnClickListener never gets called.

remove this code

textView.setMovementMethod(LinkMovementMethod.getInstance());

and manually add the following a link

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Handle click
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);

    }
});

It is not possible to tell from the snippet of code you have pasted. Please show more code. For instance, what does your declaration of textView look like?

Do you have an onClick listener on a parent view that might be overriding the text view? Also youmight want to try not using an anonymous inner class as your listener an instead use the view.OnClickListener below for click events.

myActivity extends Activity implements View.OnClickListener {
    @Override
    publiv void onClick(View v) {
        if(V.getId() == textViewId) {
             // do work
        }
    }
}

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