在 TextView 中添加另一个 Word Android

Add another Word in TextView Android

我的 RecyclerView 中有一个句子有 3 个 TextView。图片如下:

在那张图里,我一个句子是3个TextView,还有“1”个"HOT, MORE CHILLI"和"Pizza"。这是我的 RecyclerView 绑定代码:

try {
    view.txtArticlesName.setText(/*orderList.getJSONObject(position).getString("quantityValue") +*/
                            /*orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\"\[\]]", "") + */
                                    orderList.getJSONObject(position).getString("bezeich"));
   view.txtQty.setText(orderList.getJSONObject(position).getString("quantityValue"));
   view.txtReqList.setText(orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\"\[\]]", ""));

   } catch (JSONException e) {
                    e.printStackTrace();
   }

我想动态加入 TextView 中只有一个“TextView”的所有内容。我会试试这个:

view.txtArticlesName.setText(/*orderList.getJSONObject(position).getString("quantityValue") +*/
                            /*orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\"\[\]]", "") + */
                                    orderList.getJSONObject(position).getString("bezeich"));

但是它不起作用,它没有绑定数据,所以 TextView 只显示默认文本 "Hello World"。 TextView可以做到吗?我也读过 Spannable,但我不知道如何在一个 TextView 中添加新词。或者还有另一种方法可以做到这一点?任何建议和答案都会对我有所帮助。先谢谢了。

只需在您的 TextView 中连接字符串。

String string1 = "Hello", string2 = "HOT CHILLI", string3 = "PIZZA";

TextView textView = (TextView) findViewById(R.id.your_textView);
textView.setText(string1.concat(string2).concat(string3));

或者您也可以将其附加到现有 TextView 的文本中。

textView.setText(textView.getText().toString.concat(string2));

编辑:

在 String 变量中从服务器收集数据,然后将这些变量传递给 TextView。

String string1, string2, string3;


try {
    string1 = orderList.getJSONObject(position).getString("quantityValue");
    string2 = orderList.getJSONObject(position).getString("spesial-request").replaceAll("[\\"\[\]]", "");
    string3 = orderList.getJSONObject(position).getString("bezeich"));
    String final = string1.concat(string2).concat(string3);
    view.txtView.setText(final);
} catch (JSONException e) {
    e.printStackTrace();
}

使用 '+' 运算符或 StringBuilder 连接 2 个或更多字符串并将结果字符串设置为单个文本视图。

如果你想让文本的某些部分具有不同的字体、颜色、大小、粗体等,你可以使用 Spannable 字符串或 Html.fromHtml()

您可以像下面这样制作和使用 spannable String :

 private void addSpannableString(){

        //"1" "HOT, MORE CHILLI" and "Pizza"
        String one= "1";
        String two=  "HOT, MORE CHILLI";
        String three= "Pizza";
        String mergeString= one + two + three;
        Spannable spannable = new SpannableString( mergeString );
        StyleSpan boldSpan = new StyleSpan( Typeface.BOLD );
        spannable.setSpan( boldSpan, mergeString.indexOf(two), mergeString.indexOf(three), Spannable.SPAN_INCLUSIVE_INCLUSIVE );
        textview.setText(mergeString);
    }

有很多方法可以制作 Spans,包括颜色和字体

使用 TextView.append(CharSequence text) 添加更多文本。

Append the specified text to the TextView's display buffer, upgrading it to BufferType.EDITABLE if it was not already editable


我有点失望地解决了这个问题。这是我的最终代码:

try {
    view.txtArticlesName.setText(orderList.getJSONObject(position).getString("quantityValue") + " " +
        orderList.getJSONObject(position).optString("spesial-request").replaceAll("[\\"\[\]]", "") + " " +
        orderList.getJSONObject(position).getString("bezeich"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

使用 getString :

使用 optString :

我将 getString 更改为 optString 并且运算符 + 现在可以工作了。我不完全理解为什么 getString 不起作用但 optString 起作用。

非常感谢大家的回答,我+1

GetStringOptString的区别是:

来自Documentation

OptString returns the empty string ("") if the key you specify doesn't exist. GetString on the other hand throws a JSONException.

如果数据丢失是错误的,请使用 getString,如果您不确定它是否会出现,请使用 optString