【问题标题】:TextView performClick() doesn't click a linkTextView performClick() 不点击链接
【发布时间】:2018-04-05 11:24:03
【问题描述】:

我有一个TextViewandroid: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" />

出于某种原因,我想打开在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中设置TextView可点击
  • @Danger,谢谢,但没有帮助。

标签: android textview


【解决方案1】:

只需在您的 xml 中使用此 android:autoLink="web" 即可。它对我有用。让我知道它是否有效

【讨论】:

  • 谢谢,但这对我没有帮助,请查看更新。
【解决方案2】:

我们需要重写 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;
 }

【讨论】:

  • 抱歉,我在哪里可以覆盖它?
  • 对不起,这样做-add this android:onClick="onClick" then 你还必须实现 View.OnClickListener 并且在 onClick 方法中可以使用intent Intent intent = new Intent(android.content.Intent.ACTION_VIEW); intent.setData(Uri.parse("youraddress.com")); startActivity(intent); 我已经测试过了,效果很好
  • 或点击一段文本(不是整个 TextView),您可以使用 Html 或 Linkify(两者都创建打开 url 的链接,但不是应用程序中的回调)。跨度>
  • 可能这是一个很好的解决方案,但它失败并出现异常(startActivity 行):“原因:android.content.ActivityNotFoundException:未找到处理 Intent { act=android.intent.action 的活动.VIEW dat=youraddress.com }".
  • 为了解决这个问题,我们可以编写一个正确的 URL,例如:intent.setData(Uri.parse("https://www.facebook.com/abc/"));
【解决方案3】:

使用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"。它可能会起作用。还要确保使用autoLinksetMovementMethod。不要同时使用它们。

编辑 2

我知道你想在你的 textView 上使用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);
    }
}

这符合您的要求。检查它是否有效。

【讨论】:

  • 谢谢,但这对我没有帮助,请查看更新。
  • 我尝试添加android:linksClickable="true"并删除text.setMovementMethod(LinkMovementMethod.getInstance());,但没有帮助,抱歉。
  • 谢谢。类似的答案提供了@PAWAN LAKHOTIA。我认为如果在onClick 事件(用于 URL、电子邮件、电话)上添加一些操作,它将起作用。
【解决方案4】:

不幸的是,您想要实现的目标是不可能的。基于 textView.performClick() 的文档:

如果已定义,则调用此视图的 OnClickListener

由于您尚未定义任何 onClickListener,因此这些方法将不起作用。

解决方法是定义您的自定义 onClickListener 并尝试以编程方式解析 URI 以触发适当的Intent.ACTION_VIEW

【讨论】:

  • 哦!谢谢。这么多时间已经浪费了。我将按照 PAWAN LAKHOTIA 和 Abhi 的要求创建一个听众。
  • 是的。您可以创建一个侦听器。但请注意,上述方法可能不适用于链接类型PHONE_NUMMBER。您可能需要仔细解析内容并触发ACTION_VIEWACTION_CALL
  • 太棒了!享受编码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 2016-04-07
  • 2011-02-13
  • 2012-03-13
相关资源
最近更新 更多