在TextView Android中添加另一个单词

在TextView Android中添加另一个单词,android,textview,spannable,Android,Textview,Spannable,在我的RecyclerView中,我有一个带有3TextView的句子。图片如下: 在那张图片中,我在3TextView中有一句话,有“1”“辣的,更多的辣椒”和“比萨饼”。这是我的RecyclerView绑定代码: try { view.txtArticlesName.setText(/*orderList.getJSONObject(position).getString("quantityValue") +*/ /*orde

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

在那张图片中,我在3
TextView
中有一句话,有“1”“辣的,更多的辣椒”和“比萨饼”。这是我的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
可以这样做吗?我也读过,但我不知道如何在一个
TextView
中添加新词。还是有别的办法?任何建议和回答都会对我有帮助。感谢您的光临。

只需在您的文本视图中输入字符串即可

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

TextView textView = (TextView) findViewById(R.id.your_textView);
textView.setText(string1.concat(string2).concat(string3));
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();
}
或者也可以将其附加到现有TextView的文本中

textView.setText(textView.getText().toString.concat(string2));
编辑:

从服务器收集字符串变量中的数据,然后将这些变量传递给TextView

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

TextView textView = (TextView) findViewById(R.id.your_textView);
textView.setText(string1.concat(string2).concat(string3));
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();
}

只需在TextView中添加字符串

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

TextView textView = (TextView) findViewById(R.id.your_textView);
textView.setText(string1.concat(string2).concat(string3));
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();
}
或者也可以将其附加到现有TextView的文本中

textView.setText(textView.getText().toString.concat(string2));
编辑:

从服务器收集字符串变量中的数据,然后将这些变量传递给TextView

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

TextView textView = (TextView) findViewById(R.id.your_textView);
textView.setText(string1.concat(string2).concat(string3));
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
string或
Html.fromHtml()

使用
'+'
操作符或
StringBuilder
将两个或多个字符串连接起来,并将结果字符串设置为单个文本视图


如果您希望文本的某些部分具有不同的字体、颜色、大小、粗体等,您可以使用
Spannable
string或
Html.fromHtml()

您可以创建并使用span nable 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);
    }

有许多方法可用于创建跨距,包括颜色和字体

您可以创建并使用如下可扩展字符串:

 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);
    }
有许多方法可以使跨距(包括颜色和字体)用于添加更多文本

将指定的文本追加到TextView的显示缓冲区,升级 如果尚未编辑,则将其设置为BufferType.EDITABLE


用于向其中添加更多文本

将指定的文本追加到TextView的显示缓冲区,升级 如果尚未编辑,则将其设置为BufferType.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:

我用
optString
更改
getString
,操作符
+
立即工作。我不完全理解为什么
getString
不工作,但
optString
工作


非常感谢你给我的一切,+1我有点失望地解决了这个问题。这里是我的最终代码:

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:

我用
optString
更改
getString
,操作符
+
立即工作。我不完全理解为什么
getString
不工作,但
optString
工作


非常感谢您为我所做的一切,+1

GetString和OptString之间的区别是:

如果指定的键不存在,则OptString返回空字符串(“”) 存在。另一方面,GetString抛出一个JSONException


如果数据丢失是一个错误,请使用
getString
,如果您不确定它是否存在,请使用
optString

getString和
optString
之间的区别是:

如果指定的键不存在,则OptString返回空字符串(“”) 存在。另一方面,GetString抛出一个JSONException


如果数据丢失是一个错误,请使用
getString
,如果您不确定它是否存在,请使用
optString

可能您有异常。这段代码永远不会执行。只需查看日志cat或使用日志来确保所有这些代码都是正确的executed@farazkhonsari我的日志没有显示任何错误。当我从另一个RecyclerView点击比萨饼或其他食物时,它的成功广告就会出现在这个RecyclerView上,但是文本视图只显示“Hello World”,也许你有例外。这段代码永远不会执行。只需查看日志cat或使用日志来确保所有这些代码都是正确的executed@farazkhonsari我的日志没有显示任何错误。当我从另一个RecyclerView点击比萨饼或其他食物时,它的成功广告会出现在这个RecyclerView上,但是文本视图只显示“Hello World”它不起作用,我的
TextView
不会绑定我服务器上的数据谢谢你的回答,如果我们仍然使用
getString
,我认为它不起作用。我把我的答案贴在下面。再次感谢它不起作用,我的
TextView
没有绑定我服务器上的数据谢谢你的回答,如果我们仍然使用
getString
,我认为它不起作用。我把我的答案贴在下面。再次感谢+接线员这对我不起作用,我不知道为什么。我会试试你关于字符串生成器+运算符的建议,它对我不起作用,我不知道为什么。我将尝试你关于字符串生成器的建议如果是这样,为什么我们不总是使用
optString
?当需要显示数据时..应该使用Getstring..这对调试也很有帮助..如果JOSNMy
TextView
show“Hello World”中缺少任何值,因为没有带“quantityValue”的键字符串,我现在就得到了它。感谢您的精彩解释+1如果是这样,为什么我们不总是使用
optString
?当需要显示数据时..应该使用Getstring..这对调试也很有帮助..如果JOSNMy
TextView
show“Hello World”中缺少任何值,因为没有带“quantityValue”的键字符串,我现在就得到了它。感谢您对+1的精彩解释