Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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
Java Android上TextView中的多个可点击链接_Java_Android_Android Layout_Textview - Fatal编程技术网

Java Android上TextView中的多个可点击链接

Java Android上TextView中的多个可点击链接,java,android,android-layout,textview,Java,Android,Android Layout,Textview,我正试图在文本视图中添加多个链接,这与谷歌和Flipboard在下面的条款和条件以及隐私政策中所做的类似,如下面的屏幕截图所示: 到目前为止,我无意中使用了这种方法 textView.setText(Html.fromHtml(myHtml)); setMovementMethod(LinkMovementMethod.getInstance()) 其中myHtml是a href 但它不能给我所需的控制权,例如发射碎片等 在下面的两个例子中,你知道他们是如何做到这一点的吗 一个简单的解决方法是

我正试图在文本视图中添加多个链接,这与谷歌和Flipboard在下面的条款和条件以及隐私政策中所做的类似,如下面的屏幕截图所示:

到目前为止,我无意中使用了这种方法

textView.setText(Html.fromHtml(myHtml)); setMovementMethod(LinkMovementMethod.getInstance())

其中myHtml是a href

但它不能给我所需的控制权,例如发射碎片等

在下面的两个例子中,你知道他们是如何做到这一点的吗


一个简单的解决方法是使用多个标签。每个链接一个标签,文本一个标签

[TermsOfServiceLabel][andLabel][PrivacyPolicy]
还是只需要使用一个标签?

您可以使用(android.text.Spannable、java.util.regex.Pattern、java.lang.String)

然后,您可以使用该方案启动活动,例如,在AndroidManifest中添加该方案:


我想我现在分享这个有点晚了,但我已经实现了同样的目的

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

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" />  //#C36241 - Rust
/#C36241-锈蚀
这看起来像这样:

希望它能帮助别人。:)以下是我的解决方案:

首先,我们需要在文本视图中有可点击的链接:

  • 这是我在xml布局中的文本视图,不要添加任何链接 参数

    <TextView
            android:id="@+id/sign_up_privacy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/terms_and_privacy"/>
    
  • 现在可以单击TextView链接

    其次,我们需要处理这些点击:

  • 在我的清单文件中,我添加了“条款”和“隐私”的意图过滤器 和单实例启动模式

    <activity
                android:name=".MyActivity"
                android:launchMode="singleInstance">
                <intent-filter>
                    <category android:name="android.intent.category.DEFAULT"/>
                    <action android:name="android.intent.action.VIEW"/>
    
                    <data android:scheme="terms"/>
                    <data android:scheme="privacy"/>
                </intent-filter>
            </activity>
    
  • 使用,这可以通过以下方式实现:

    res/values/strings.xml:

    <resources>
         <string name="str_terms_and_privacy">By signing up you agree to our <a href="terms:">Terms of Service</a> and <a href="privacy:">Privacy Policy.</a></string>
    </resources>
    
    这种方法的优点是:

    • 将文本外部化为字符串资源。使您的应用程序更易于本地化
    • 可以使用
      LinksHandler
      直接处理点击事件,无需定义额外的意图过滤器
    • 更简单、更可读的代码

      • 这对我来说很有用:

        在xml中:

        <TextView
            android:id="@+id/tv_by_continuing_str"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:textSize="15sp"
            tools:text="Test msg 1 2, 3"
            android:textColor="@color/translucent_less_white3"
            android:textColorLink="@color/white"
            android:gravity="center|bottom"
            android:layout_above="@+id/btn_privacy_continue" />
        

        最好的方法是使用字符串文件

        在string.xml中

        <string name="agree_to_terms">I agree to the %1$s and %2$s</string>
        <string name="terms_of_service">Terms of Service</string>
        <string name="privacy_policy">Privacy Policy</string>
        


        什么有多个标签?文本视图?你能进一步详细说明吗?如果我理解正确,你想在一个文本视图中有多个链接,但不知道如何做到这一点?拆分TeXVIEW让你有一个Twitter的链接,这不应该是个问题。我显然可以使用3种不同的文本视图,但是还有其他的事情要考虑,比如在不同的设备上的间距,这就是为什么我要看是否有一种方法来实现这一点。如果你想使用一个文本视图,你可以忽略我的答案!这很有帮助,但是如果我不想开始活动,我可以通过侦听器点击链接吗?重要部分:Linkify.addLinks必须在tvTermsOfUse.setMovementMethod之前完成,否则它将无法工作。XML中不需要其他设置。linkify使链接显示为下划线如何删除,以反映为flipboard此操作。使用此自定义方案查看意图将仅在应用程序中,甚至其他应用程序也可以添加意图过滤器并点击此选项(我希望不是因为这容易出错)@harrane???@harrane是否可以将此链接文本设置为web url,以便在单击文本时打开网页???它不起作用。日志中有消息ActivityNotFoundException:找不到处理Intent的活动{act=android.Intent.action.VIEW dat=terms:(具有额外功能)}@sonnv1368,是否覆盖了onNewIntent(INTENTENT)在您的活动中?无论如何,我建议使用SpannableString作为更好的解决方案:@Ahmod Baraka:谢谢。如果您只想打开一个新活动,则此答案的改进代码是将清单设置到您想要打开的目标中。通过这种方式,您可以避免在当前活动中使用“singleInstance”,这可能会导致一些关键问题。如果字符串不需要翻译,则此方法效果很好。也可以与本地化一起使用:多语言只需将字符串传递到使用文本的位置。我刚做完:)是的,它会有用的。我的最后一条评论是针对RTL语言的。应用此选项后,marquee不起作用,有什么想法吗?@BeeingJk你想滚动文本吗?
        TextView privacyTextView = (TextView) root.findViewById(R.id.sign_up_privacy);
        privacyTextView.setMovementMethod(LinkMovementMethod.getInstance());
        
        <activity
                    android:name=".MyActivity"
                    android:launchMode="singleInstance">
                    <intent-filter>
                        <category android:name="android.intent.category.DEFAULT"/>
                        <action android:name="android.intent.action.VIEW"/>
        
                        <data android:scheme="terms"/>
                        <data android:scheme="privacy"/>
                    </intent-filter>
                </activity>
        
        @Override
            protected void onNewIntent(Intent intent)
            {
                if (intent.getScheme().equalsIgnoreCase(getString("terms")))
                {
                    //handle terms clicked
                }
                else if (intent.getScheme().equalsIgnoreCase(getString("privacy")))
                {
                    //handle privacy clicked
                }
                else
                {
                    super.onNewIntent(intent);
                }
            }
        
        <resources>
             <string name="str_terms_and_privacy">By signing up you agree to our <a href="terms:">Terms of Service</a> and <a href="privacy:">Privacy Policy.</a></string>
        </resources>
        
        <TextView
            android:id="@+id/view_terms_and_privacy"
            android:text="@string/str_terms_and_privacy"
            />
        
        public class MyActivity extends Activity {
            ...
            protected void onCreate(Bundle savedInstanceState) {
                ...
                TextView termsAndPrivacy = Textoo
                    .config((TextView) findViewById(R.id.view_terms_and_privacy))
                    .addLinksHandler(new LinksHandler() {
                        @Override
                        public boolean onClick(View view, String url) {
                            if ("terms:".equals(url)) {
                                // Handle terms click
                                return true;  // event handled
                            } else if ("privacy:".equals(url)) {
                                // Handle privacy click
                                return true;  // event handled
                            } else {
                                return false;  // event not handled.  continue default processing i.e. launch web browser and display the link
                            }
                        }
                    })
                    .apply();
                ...
            }
            ...
        }
        
        <TextView
            android:id="@+id/tv_by_continuing_str"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:textSize="15sp"
            tools:text="Test msg 1 2, 3"
            android:textColor="@color/translucent_less_white3"
            android:textColorLink="@color/white"
            android:gravity="center|bottom"
            android:layout_above="@+id/btn_privacy_continue" />
        
        < string name="by_continuing_str2">< ! [ CDATA[By continuing to use this app, you agree to our <a href="https://go.test.com" style="color:gray"/> Privacy Statement </a> and <a href="https://go.test.com" style="color:gray"/>Services Agreement.]]>< / string>
        
        TextView tv_by_continuing = (TextView) findViewById(R.id.tv_by_continuing);
        tv_by_continuing.setText(Html.fromHtml(getString(R.string.by_continuing_str2)));
        tv_by_continuing.setMovementMethod(LinkMovementMethod.getInstance());
        
        <string name="agree_to_terms">I agree to the %1$s and %2$s</string>
        <string name="terms_of_service">Terms of Service</string>
        <string name="privacy_policy">Privacy Policy</string>
        
        private void setTermsAndCondition() {
            String termsOfServicee = getString(R.string.terms_of_service);
            String privacyPolicy = getString(R.string.privacy_policy);
            String completeString = getString(R.string.agree_to_terms, termsOfServicee,privacyPolicy);
            int startIndex = completeString.indexOf(termsOfServicee);
            int endIndex = startIndex + termsOfServicee.length();
            int startIndex2 = completeString.indexOf(privacyPolicy);
            int endIndex2 = startIndex2 + privacyPolicy.length();
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(completeString);
            ClickableSpan clickOnTerms = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(this, "click on terms of service", Toast.LENGTH_SHORT).show();
                }
        
                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(ContextCompat.getColor(this, R.color.blue));
        
                }
            };
            ClickableSpan clickOnPrivacy = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(this, "click on privacy policy", Toast.LENGTH_SHORT).show();
                }
        
                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(ContextCompat.getColor(this, R.color.blue));
        
                }
            };
            spannableStringBuilder.setSpan(clickOnTerms, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.setSpan(clickOnPrivacy, startIndex2, endIndex2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            yourTextView.setText(spannableStringBuilder);
            yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
        }
        
        
        private fun customTextView(view: TextView) {
        
                //I agree to the Terms of service & Privacy Policy,
                val spanTxt = SpannableStringBuilder(
                    "I agree to the ")
                spanTxt.append("Terms of service")
                spanTxt.setSpan(object : ClickableSpan() {
                    override fun onClick(widget: View) {
                     
                        toast("Terms of service link clicked")
        
                    }
                }, spanTxt.length - "Term of services".length, spanTxt.length, 0)
                spanTxt.append(" and")
                spanTxt.append(" Privacy Policy")
                spanTxt.setSpan(object : ClickableSpan() {
                    override fun onClick(widget: View) {
        
                        toast("Privacy Policy link clicked")
        
                    }
                }, spanTxt.length - " Privacy Policy".length, spanTxt.length, 0)
                view.movementMethod = LinkMovementMethod.getInstance()
                view.setText(spanTxt, TextView.BufferType.SPANNABLE)
            }