TextView跑马灯与EditText共存失效

当在Android应用中,TextView跑马灯效果与EditText共存时,由于焦点冲突,可能导致跑马灯失效。为解决这一问题,可以尝试两种方法:1. 在TextView中设置滚动属性以确保跑马灯持续运行;2. 调整布局和焦点管理,避免两者直接竞争焦点。通过这些方法,可以实现两者同时正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android  TextView跑马灯与EditText共存时,犹豫焦点冲突导致的跑马灯失效,有两种解决方案:

1.TextView里设置

android:singleLine="true"//必须用这个  虽然已经过时   但是如果用maxLines=1,跑马灯会失效
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="false"
android:scrollHorizontally="true"

然后再代码中添加:

myTextView.setSelected(true);

2.自定义TextView

/**
 * Created by yjc on 2017/5/12.
 */

public class FocusedTextView extends TextView{
    public FocusedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public FocusedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public FocusedTextView(Context context) {
        super(context);
    }
    @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;
    }
}
然后xml直接用就好了:

<com.workinghours.view.FocusedTextView
        android:id="@+id/tv_marquee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"vity="center_vertical"
        android:singleLine="true"
        android:text="0000000000000000000000000000000000000000000000000000"
        android:focusable="true"
        android:scrollHorizontally="true"
        android:marqueeRepeatLimit="marquee_forever"/>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值