让TextView实现走马灯效果并避免因EditText和 AlerterDialog抢走了焦点而停止
来源:互联网 发布:ieee 802.11 mac协议 编辑:程序博客网 时间:2024/05/22 13:13
首先要牢记一点:即Android布局中默认只能有一个view获得焦点,不可能存在多个view同时获得焦点的情况.
一.
如果只需要让唯一 一个TextView实现走马灯的话,可以直接在布局文件中为该 TextView添加以下五个个属性即可:
android:singleLine=”true”单行显示
android:ellipsize=”marquee”走马灯样式
android:focusable=”true”
android:focusableInTouchMode=”true”
android:marqueeRepeatLimit=”marquee_forever”
二.避免EditText和Dialog抢占了焦点而停止
如果需要让多个TextView同时实现走马灯,或者同一布局中在编辑EditText时或在弹出了Dialog后走马灯能继续执行的话,就与上面说的一个布局中只能有一个view获得焦点矛盾了,该怎么办呢,这时需要自定义 TextView了.
public class FocusedTextView extends TextView { // 如果需要在代码中new该自定义TextView时用的constructor public FocusedTextView(Context context) { this(context, null); } // 如果需要将该自定义TextView copy qualify name到布局文件中,这个constructor就是为编译器准备的 public FocusedTextView(Context context, AttributeSet attrs) { super(context, attrs); //代码设置走马灯的五个必备属性 setEllipsize(TruncateAt.MARQUEE); setFocusable(true); setFocusableInTouchMode(true); setMarqueeRepeatLimit(-1);//-1即永远执行 setSingleLine(); }//该方法返回true,是为了让多个需要同时实现走马灯的TextView相信其本身是获得焦点的 @Override public boolean isFocused() { return true; }//避免编辑EditText时走马灯停止的必要实现 @Override protected void onFocusChanged(boolean focused, int direction,Rect previouslyFocusedRect) { if (focused) { super.onFocusChanged(focused, direction, previouslyFocusedRect); } }//避免弹出dialog后走马灯停止的必要实现 @Override public void onWindowFocusChanged(boolean hasWindowFocus) { if (hasWindowFocus) { super.onWindowFocusChanged(hasWindowFocus); } }}
0 0
- 让TextView实现走马灯效果并避免因EditText和 AlerterDialog抢走了焦点而停止
- TextView实现走马灯效果
- [Android] TextView实现走马灯效果
- [Android] TextView实现走马灯效果
- [Android] TextView实现走马灯效果
- Layout获取焦点后,如何让子控件TextView产生走马灯效果?
- android:TextView实现文字走马灯效果(欺骗系统获取持久的焦点)
- 多个TextView同时实现走马灯效果
- 多个TextView实现走马灯效果
- android TextView 走马灯效果
- 走马灯效果文字TextView
- TextView的走马灯效果
- Android TextView走马灯效果
- Android TextView 走马灯效果
- TextView 走马灯效果
- Android TextView走马灯效果
- Android textview和listview实现水平自动滚动的走马灯效果
- 老罗安卓教程 在TextView文本中实现走马灯效果
- android开发脚本之几个常用脚本sh
- 课后作业静态网页制作-仿rollingstone官网
- 设计模式-Prototype模式
- oracle 参数化算多少天之前的日期
- git使用简明教程
- 让TextView实现走马灯效果并避免因EditText和 AlerterDialog抢走了焦点而停止
- Hibernate内置标识符生成器
- Cordova编译异常小结
- webRTC在Win7 VS2015下的编译和生成
- 问题 K: 数据结构(C语言版)算法7.4至算法7.6__DFS与BFS //这题做法不正规
- 加密锁的使用技巧
- Hough变换——直线检测(投票方法实现)
- detect the encoding of files in Python
- 如何在Spring 的官网上下载 Spring Framework 和 Spring Tool Suite