第一步、创建一个自定义的类,让它继承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有聚焦的能力,其做法如下:
继承父类的聚焦函数:

@Overridepublic boolean isFocused(){    //在这里我们告诉操作系统,我已经聚焦了    return true;} 

做完这里之后,用你自定义的组件便可以实现文本自动滚动,而不用点击聚焦后才滚动。
但是,需求总是远远不能满足,小编发现这样做了之后,只能从xml对TextView的显示内容进行初始化,并不能通过Java代码初始化,因为我们在继承父类的时候并没有如下这个方法:

this.findViewById(R.id.textView).setText("");

这样结果实在是让人无语,上面的精力白费了,没关系,让小编我来带你飞。

第四步、继承父类的setText()方法和getText()方法,代码如下:

@Overridepublic void setText(CharSequence text, BufferType type) {    // TODO Auto-generated method stub    super.setText(text, type);}@Override@CapturedViewPropertypublic 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}@Overridepublic boolean isFocused(){    return true;} @Override@CapturedViewPropertypublic CharSequence getText() {    // TODO Auto-generated method stub    return super.getText();}@Overridepublic void setText(CharSequence text, BufferType type) {    // TODO Auto-generated method stub    super.setText(text, type);}   public final void setText(String text) {    setText(text, mBufferType);   }}

更多相关文章

  1. Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码
  2. android 10.0 以上隐藏方法无法调用 #Accessing hidden method
  3. AndroidStudio配置使用lambda
  4. Android(安卓)AsyncTask 浅析(源代码取自 API level 23)
  5. Android(安卓)系统桌面Launcher 开发
  6. android Forlinux framworks学习资料
  7. Android(安卓)Handler机制5--消息发送
  8. android onNewIntent
  9. [置顶] android 第三方jar库 反射得到自己的资源ID

随机推荐

  1. Android使用ListView批量删除item的方法
  2. Android(安卓)md5加密与php md5加密一致
  3. Spring 是如何造出一个 Bean 的
  4. Android(安卓)数据库一次创建多个表
  5. Android(安卓)模拟器如何安装apk以及如何
  6. Androidstudio打包jar和arr包
  7. android 下之hello world c 手动编译
  8. 在eclipse中导入项目时的问题
  9. 初学了解MVC在android中的应用
  10. Android(安卓)MenuItem 设置文字颜色-Tex