android TextView自动滚动以及Java中改变显示内容
2015-10-16 21:37
309 查看
图1 TextView实现自动滚动效果图
第一步、创建一个自定义的类,让它继承TextView,然后添加默认方法,为以后扩展你的TextView,添加所有方法,其方法分别是:
public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public AutoScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public AutoScrollTextView(Context context) { super(context); // TODO Auto-generated constructor stub }
第二步、要实现文本从一生下来就自动滚动,那么就需要文本聚焦,而Android的TextView从生下来是没有聚焦的,要实现自动聚焦,那就自己复写TextView,这也是我现在讲解的东西。为了能够实现自动滚动,我可以采用欺骗的手段,来达到目的,其原理是在我们的TextView一生下来时,我就让它聚焦,就像我们一生下来的时候明明是男孩,但是我通过人工手术变成了女孩,其本质还是男孩,只是欺骗了别人,我们就欺骗操作系统,让TextView有聚焦的能力,其做法如下:
继承父类的聚焦函数:
@Override public boolean isFocused(){ //在这里我们告诉操作系统,我已经聚焦了 return true; }
做完这里之后,用你自定义的组件便可以实现文本自动滚动,而不用点击聚焦后才滚动。
但是,需求总是远远不能满足,小编发现这样做了之后,只能从xml对TextView的显示内容进行初始化,并不能通过Java代码初始化,因为我们在继承父类的时候并没有如下这个方法:
this.findViewById(R.id.textView).setText("");
这样结果实在是让人无语,上面的精力白费了,没关系,让小编我来带你飞。
第四步、继承父类的setText()方法和getText()方法,代码如下:
@Override public void setText(CharSequence text, BufferType type) { // TODO Auto-generated method stub super.setText(text, type); } @Override @CapturedViewProperty public CharSequence getText() { // TODO Auto-generated method stub return super.getText(); }
然后自定义一个setText()方法,必须加final,编译器也会提示你,代码如下:
public final void setText(String text){ setText(text,mBufferType); }
到这里之后,你会发现,要实现自己的setText()方法,只能调用父类的setText(CharSequence text, BufferType type)方法,因为父类的方法中并没参数类型为String的方法,而直接用CharSequence会出现错误,所以我们只能去调用setText(CharSequence text, BufferType type)方法,而调用这个方法时需要一个mBufferType参数,我们并不知道是啥,当然我们也不用管,在父类中找到这个变量,考过来,代码如下:
private BufferType mBufferType = BufferType.NORMAL;
至此,一个省下来就可以自己滚出家的,又能动态改变显示内容的TextView就被我们造出来了,当你需要别的方法,以同样的方法,这里只是给初学者一个小小的自定义组件的参考,由于小编才疏学浅,如有错误,还望指正,万分感谢!
为方便初学者理解,以下是完整代码:
public class AutoScrollTextView extends TextView {
private BufferType mBufferType = BufferType.NORMAL;
public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public AutoScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public AutoScrollTextView(Context context) { super(context); // TODO Auto-generated constructor stub }
@Override
public boolean isFocused(){
return true;
}
@Override
@CapturedViewProperty
public CharSequence getText() {
// TODO Auto-generated method stub
return super.getText();
}
@Override
public void setText(CharSequence text, BufferType type) {
// TODO Auto-generated method stub
super.setText(text, type);
}
public final void setText(String text) {
setText(text, mBufferType);
}
}
相关文章推荐
- Android 旋转字体 实现(应用角标,如:新,火等关键字)
- Android数据缓存(转)
- android 17 activity生命周期
- 以Android环境为例的多线程学习笔记———-Callable与Future
- android学习
- Android学习之Activity生命周期
- android学习资源
- Android性能优化之GPU过度绘制与图形渲染优化
- Android的十六进制颜色值
- Android视频技术点
- Ubuntu下Android开发环境的搭建
- Android基础入门教程——8.3.2 绘图类实战示例
- Android apk反编译及AS代码混淆方法
- android 图片点击一下就放大到全屏,再点一下就回到原界面
- android资源文件说明
- Android应用开发性能优化完全分析
- Android监听键盘显示和隐藏
- Android ViewGroup系列控件的使用
- Android笔记(三十九) Android中的数据存储——SQLite(一) create
- android学习3:Context