处理本地化的字符串在单个TextView中包含一个链接 [英] Handle localized string contains a link in a single TextView

查看:54
本文介绍了处理本地化的字符串在单个TextView中包含一个链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在strings.xml文件中有一个字符串.单击此字符串的某些部分将重定向到任务.那部分是根据字符串的索引来完成的.

i have a string in strings.xml file. 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.

英文字符串的代码:

    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. e.g.

 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.

这篇关于处理本地化的字符串在单个TextView中包含一个链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆