简体   繁体   中英

TextView in Android doesn't show any text after '/' character

I have a TextView showing the name of a parameter, the value of the parameter and a unit. (Like "speed 5 m/s")

Since I need the value to change it's color i am using a Handler to switch between two Strings. Both of these strings have html-code injected, so i can change the color without the need to have more than one TextView.

My problem now is that if i have a dash ('/') inside my string, all characters after it will not be shown.

If i replace the slash inside my string it works. But that's not really solution.

private void setBlinkText(){
try{
    strBlinkOff = "speed <font color='#fafafa'>12</font> m/s";
    strBlinkOn = "speed  <font color='#212121'>12</font> m/s";


        m_displayLine.setText(strBlinkOff.substring(0, strBlinkOff.indexOf('<')));

        m_displayLine.append(Html.fromHtml(strBlinkOff.substring(strBlinkOff.indexOf('<'), strBlinkOff.indexOf("font>")), Html.FROM_HTML_MODE_LEGACY));

        m_displayLine.append(strBlinkOff.substring(strBlinkOff.indexOf("font>") + 5));


    m_blinkHandler = new Handler();
    final String strFinalBlinkOn = strBlinkOn;
    m_blinkHandler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {

                m_displayLine.setText(strFinalBlinkOn.substring(0, strFinalBlinkOn.indexOf('<')));

                m_displayLine.append(Html.fromHtml(strFinalBlinkOn.substring(strFinalBlinkOn.indexOf('<'), strFinalBlinkOn.indexOf("font>")), Html.FROM_HTML_MODE_LEGACY));

                m_displayLine.append(strFinalBlinkOn.substring(strFinalBlinkOn.indexOf("font>") + 5));


            m_blinkHandler.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    setBlinkText(p_strMessage);
                }
            }, 800);
        }
    }, 300);
} catch(Exception e)
{
    Toast.makeText(getContext(), "Error", Toast.LENGTH_SHORT).show();
}

}

m_displayLine should show "speed 12 m/s" but instead just shows "speed 12 m/"

Edit: The TextView looks like this:

    <TextView
        android:id="@+id/ToolTextLine2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:background="@color/accentGreenLight"
        android:fontFamily="@font/lordmeg09"
        android:maxLength="16"
        android:maxLines="1"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:textColor="#2d373c"
        android:textSize="28sp"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guidelineTool2" />

It works without the autosizing and a smaller textsize. But why does it get cut off?

Looks like the maxLines attribute was getting ignored cause i was appending the text to my TextView after already setting it. I fixed it by setting

m_displayLine2.setLines(1);

again after appending all parts of my string.

Try using &sol; instead of /

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