TextView for Android Development

TextView to achieve the effect of riding lamp

Add the following properties to TextView:

1.singleLine = "true": one line displays all text. This property is out of date, but the use of lines = "1" is invalid

2.scrollHorizontally = "true": horizontal scrolling

3. Focuseable = "true": focus can be obtained

4.focusableInTouchMode = "true": touch screen device can obtain focus

be careful:

    if multiple textviews want to achieve the riding lamp effect at the same time (for example, TextView in each Item in ListView), it cannot be achieved simply by adding the above properties, because only one TextView can get the focus at the same time.

Therefore, you need to rewrite TextView:

public class MarqueeTextView extends TextView
{
    public MarqueeTextView(Context context)
    {
        this(context, null);
    }
    public MarqueeTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        setFocusable(true);
        setFocusableInTouchMode(true);

        setSingleLine();
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
    }
    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        setFocusable(true);
        setFocusableInTouchMode(true);

        setSingleLine();
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
    {
        if (focused)
        {
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
        }
    }
    @Override
    public void onWindowFocusChanged(boolean focused)
    {
        if (focused)
        {
            super.onWindowFocusChanged(focused);
        }
    }
    @Override
    public boolean isFocused()
    {
        return true;
    }
}

Posted by JamieWAstin on Tue, 05 May 2020 08:07:35 -0700