简体   繁体   English

不会调用TextView的OnClickListener

[英]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 . 我的Android应用程序中有几个文本链接 ,它们是电子邮件地址Web链接 I use the following code snippet to set them up (example for a web link): 我使用以下代码片段进行设置(例如Web链接):

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 . TextView 没有设置特殊属性,例如android:linksClickableandroid:clickableandroid:focusable android:linksClickable Its style inherits from @android:style/TextAppearance.Small . 其样式继承自@android:style/TextAppearance.Small

For some reason the OnClickListener never gets called. 由于某些原因,永远不会调用OnClickListener

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? 例如,您对textView的声明是什么样的?

Do you have an onClick listener on a parent view that might be overriding the text view? 在父视图上是否有一个onClick侦听器,它可能会覆盖文本视图? Also youmight want to try not using an anonymous inner class as your listener an instead use the view.OnClickListener below for click events. 另外,您可能希望不要将匿名内部类用作侦听器,而应将下面的view.OnClickListener用作单击事件。

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

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

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