0

android:autoLink="web"在我的 TextViews 中使用将 URL 转换为可点击的链接。这很好用。由于链接是用户生成的,我想事先通过对话框询问用户,他们是否真的想打开这个链接。

我还没有找到任何东西,有没有办法在将其转发到典型ACTION_VIEW意图之前拦截该点击并显示一个对话框?

4

1 回答 1

0

尝试添加到您的 TextView 属性,例如:

<TextView
    android:text="http://www.google.com"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:onClick="onUrlClick"
    android:linksClickable="false"
    android:clickable="true"
    />

然后覆盖 onClick 方法,如:

    public void onUrlClick(final View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            TextView myTextView = (TextView)view;
            String myUrl = String.valueOf(myTextView.getText());
            Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse(myUrl) );
            startActivity( browse );
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

它对我有用。这只是示例,为了获得良好的实践,您应该将创建与 onClick 方法分开。

于 2014-10-21T16:57:31.180 回答