【问题标题】:Show dialog before opening autoLink in TextView在 TextView 中打开 autoLink 之前显示对话框
【发布时间】:2014-10-21 16:17:42
【问题描述】:

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

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

【问题讨论】:

    标签: android android-intent hyperlink textview autolink


    【解决方案1】:

    尝试添加到您的 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 方法分开。

    【讨论】:

    • 但我的 TextView 可能包含多个 URL。这还能用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2013-04-05
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多