简体   繁体   English

处理本地化字符串包含单个 TextView 中的链接

[英]Handle localized string contains a link in a single TextView

i have a string in strings.xml file.我在strings.xml 文件中有一个字符串。 clicks on some part of this string redirect to a task.单击此字符串的某些部分重定向到任务。 that some part where made based on the index of the string.根据字符串的索引制作的某些部分。

Now i am trying to translate it to french but i am getting index out of bound exception as its less then the length of English strings.现在我正在尝试将它翻译成法语,但我得到的索引超出了边界异常,因为它小于英语字符串的长度。

Could anyone please say, what will be the best way to handle this scenario?任何人都可以说,处理这种情况的最佳方法是什么?

String separation is one thing we can do.字符串分离是我们可以做的一件事。

but i want to handle it in one text view itself.但我想在一个文本视图中处理它。

Code for the English String:英文字符串的代码:

    SpannableString spannableString = new SpannableString(getResources().getString(R.string.desc));
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Log.v("dosomething", "dosomething");
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            Log.v("task one", "task one");
        }
    };

    spannableString.setSpan(clickableSpan, 87, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    mDesc.setText(spannableString);
    mDesc.setMovementMethod(LinkMovementMethod.getInstance());

Then you will have to detect the language that the device is using whether it uses the french strings or the english strings then, put this in each condition defining the length for the given strings file.然后,您必须检测设备使用的语言是使用法语字符串还是英语字符串,然后将其放入定义给定字符串文件长度的每个条件中。 eg例如

 if (Locale.getDefault().getLanguage().equals("en")) {
     spannableString.setSpan(clickableSpan, 87, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 }
 else if (Locale.getDefault().getLanguage().equals("fr")) {
     spannableString.setSpan(clickableSpan, 50, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 }

just change the start and the end length depending on the string that is in the strings file.只需根据字符串文件中的字符串更改开始和结束长度即可。

You can use annotations and then set the ClickableSpan on the annotated part.您可以使用注释,然后在注释部分上设置ClickableSpan That way, the translations should contain that information.这样,翻译应该包含该信息。

Define localized strings in resources like this:在资源中定义本地化字符串,如下所示:

<string name="complete_phrase">By clicking on continue, you agree to the terms and conditions and privacy policy.</string>
<string name="tos_part">terms and conditions</string>
<string name="pp_part">privacy policy</string>

tos_part and pp_part contain the text that is clickable in the complete_phrase . tos_partpp_part包含在complete_phrase可点击的文本。

Then build the span and set it to a TextView like this:然后构建跨度并将其设置为 TextView,如下所示:

val someTextView = ...
val phrase = resources.getString(R.string.complete_phrase)
val spannable = SpannableStringBuilder(phrase)
    .apply {
        val tosPart = resources.getString(R.string.tos_part)
        val tosStart = phrase.indexOf(tosPart)
        setSpan(object : ClickableSpan() {

            override fun onClick(textView: View) {
                view.context.startActivity(Intent(view.context, ThermsAndConditionsActivity::class.java))
            }
        }, tosStart, tosStart + tosPart.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

        val ppPart = resources.getString(R.string.pp_part)
        val ppStart = phrase.indexOf(ppPart)
                setSpan(object : ClickableSpan() {

            override fun onClick(textView: View) {
                            view.context.startActivity(Intent(view.context, DataPrivacyActivity::class.java))
                        }
        }, ppStart, ppStart + ppPart.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
    }
someTextView.setText(spannable)
someTextView.movementMethod = LinkMovementMethod.getInstance()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM