简体   繁体   中英

Add multiple text to a TextView

I have created a TextView with similar details:

 <TextView
            android:id="@+id/tAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tHeadline"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="4dp"
            android:gravity="center" 
            android:hint="Age"
                        android:textSize="18sp"

            android:textColor="#000" />

I would like to set text for multiple strings. such as

mUserAgeRetrieved.setText("Age:");

        mUserAgeRetrieved.setText(String.valueOf(objects.get(i).getInt("UserAge")));

Hence, the result of the above would be Age: (age that is generated from Parse).

Another Instance

mUserHeadlineRetrieved = (TextView) findViewById(R.id.tHeadline);
mUserAgeRetrieved.setText("Headline:");

                mUserHeadlineRetrieved.setText(objects.get(i).get("Headline")
                        .toString());

Hence, the result of the above would be Headline: (Headline that is generated from Parse). The problem I have is that It chose the last setText, and does not combined both of them.

Lets say I wanted to have one of them bold like Age: (bold) 25 (non-bold)

If you need any clarification, let me know.

mUserAgeRetrieved.setText("Age: " + String.valueOf(objects.get(i).getInt("UserAge")));

or

mUserHeadlineRetrieved.setText("Headline: " + objects.get(i).get("Headline").toString());

You just need to use regular string concatenation.

Of course it doesn't! setText sets new text.

The solution is very simple.

textView.setText("This: ");
textView.append("works");

textView should say "This: works"

Hope this helps,

good luck :)

您可以将两个文本都设置为单个字符串,然后进行设置,如下所示:

    mUserAgeRetrieved.setText(Html.fromHtml("<b>" + "Age:" + "</b>" + String.valueOf(objects.get(i).getInt("UserAge"))));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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