Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android textview中捕获http链接单击事件_Android_Url_Textview - Fatal编程技术网

在android textview中捕获http链接单击事件

在android textview中捕获http链接单击事件,android,url,textview,Android,Url,Textview,我在android textview中有一个链接。我无法捕获链接单击事件 String text = "http:://www.google.com is a google link"; textview.setText(text); textview.setText("http://google.com is google website and http://youtube.com is youtube site"); setLinkclickEvent(textview, new Han

我在android textview中有一个链接。我无法捕获链接单击事件

String text = "http:://www.google.com is a google link";
textview.setText(text);
textview.setText("http://google.com is google website and http://youtube.com is youtube site");
setLinkclickEvent(textview, new HandleLinkClickInsideTextView() {
    public void onLinkClicked(String url) {
          // Here I added my code
    }
});
“http:://www.google.com”此字符串跨度可在textview中单击。我想捕捉那个特定的点击事件

String text = "http:://www.google.com is a google link";
textview.setText(text);
textview.setText("http://google.com is google website and http://youtube.com is youtube site");
setLinkclickEvent(textview, new HandleLinkClickInsideTextView() {
    public void onLinkClicked(String url) {
          // Here I added my code
    }
});
我尝试了以下方法

public static void setTextView(TextView text, CharSequence sequence) {
    UoloLogger.i(TAG, "Setting string :: "+sequence);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for(URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }
    text.setText(strBuilder);
    text.setMovementMethod(LinkMovementMethod.getInstance());
}

public static void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            UoloLogger.i(TAG, span.getURL());
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
我开始使用setTextView()方法将文本设置到textview中。我得到的URLSpan数组是空的,即使我有链接

String text = "http:://www.google.com is a google link";
setTextView(textView, text);

对不起,英语不好。我想,我已经解释了我的问题。有人能帮我吗

如果要在单击textview后打开链接,有两个选项:

  • 使用java代码:

    Spanned text = Html.fromHtml("<u>GOOGLE.COM</u>");
    textView.setText(text);
    Uri uri = Uri.parse("http://shopwhere.com.au/");
    Intent webIntent = new Intent(Intent.ACTION_VIEW,uri);
    // Create and start the chooser
    Intent chooser = Intent.createChooser(webIntent, "Open with");
    startActivityForResult(chooser,0);
    
    private void customTextView(TextView view) {
            SpannableStringBuilder spanTxt = new SpannableStringBuilder(
                    "I agree to the ");
            spanTxt.append("Term of services");
            spanTxt.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(getApplicationContext(), "Terms of services Clicked",
                            Toast.LENGTH_SHORT).show();
                }
            }, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0);
            spanTxt.append(" and");
            spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0);
            spanTxt.append(" Privacy Policy");
            spanTxt.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(getApplicationContext(), "Privacy Policy Clicked",
                            Toast.LENGTH_SHORT).show();
                }
            }, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0);
            view.setMovementMethod(LinkMovementMethod.getInstance());
            view.setText(spanTxt, BufferType.SPANNABLE);
        } 
    
    span text=Html.fromHtml(“GOOGLE.COM”);
    textView.setText(text);
    Uri=Uri.parse(“http://shopwhere.com.au/");
    Intent webIntent=新的Intent(Intent.ACTION\u视图,uri);
    //创建并启动选择器
    Intent chooser=Intent.createChooser(webIntent,“打开时使用”);
    startActivityForResult(选择器,0);
    
  • 使用XML:

    使用android:autoLink=“web”内部文本视图标记。您还可以更改链接颜色
    android:textColorHighlight=“@android:color/transparent”
    android:textColorLink=“@color/white”


  • 您可以使用SpannableStringBuilder实现同样的功能

    只需初始化要添加2个或更多侦听器的TextView,然后将其传递给我创建的以下方法:

    示例代码:

    Spanned text = Html.fromHtml("<u>GOOGLE.COM</u>");
    textView.setText(text);
    Uri uri = Uri.parse("http://shopwhere.com.au/");
    Intent webIntent = new Intent(Intent.ACTION_VIEW,uri);
    // Create and start the chooser
    Intent chooser = Intent.createChooser(webIntent, "Open with");
    startActivityForResult(chooser,0);
    
    private void customTextView(TextView view) {
            SpannableStringBuilder spanTxt = new SpannableStringBuilder(
                    "I agree to the ");
            spanTxt.append("Term of services");
            spanTxt.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(getApplicationContext(), "Terms of services Clicked",
                            Toast.LENGTH_SHORT).show();
                }
            }, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0);
            spanTxt.append(" and");
            spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0);
            spanTxt.append(" Privacy Policy");
            spanTxt.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(getApplicationContext(), "Privacy Policy Clicked",
                            Toast.LENGTH_SHORT).show();
                }
            }, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0);
            view.setMovementMethod(LinkMovementMethod.getInstance());
            view.setText(spanTxt, BufferType.SPANNABLE);
        } 
    
    在XML中,使用android:textColorLink添加您选择的自定义链接颜色。像这样:

      <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="TextView"
         android:textColorLink="#C36241" /> 
    
    
    
    在此之后,我只使用了sendclick事件的方法

    String text = "http:://www.google.com is a google link";
    textview.setText(text);
    
    textview.setText("http://google.com is google website and http://youtube.com is youtube site");
    setLinkclickEvent(textview, new HandleLinkClickInsideTextView() {
        public void onLinkClicked(String url) {
              // Here I added my code
        }
    });
    

    我可以打开链接。我想捕获点击的链接,并执行一些代码。按照Arunkumar给出的步骤执行: