当前位置: 移动技术网 > IT编程>移动开发>Android > Android基于TextView不获取焦点实现跑马灯效果

Android基于TextView不获取焦点实现跑马灯效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

本文实例讲述了android基于textview不获取焦点实现跑马灯效果。分享给大家供大家参考,具体如下:

1. 写一个类继承textview

package com.example.tt;
import android.content.context;
import android.graphics.rect;
import android.util.attributeset;
import android.widget.textview;
public class scrollingtextview extends textview {
  public scrollingtextview(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);
  }
  public scrollingtextview(context context, attributeset attrs) {
    super(context, attrs);
  }
  public scrollingtextview(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;
  }
}

2. xml 中增加属性

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<com.example.tt.scrollingtextview
   android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginleft="80dip"
    android:layout_marginbottom="25dip"
    android:textsize="25sp"
    android:singleline="true"
    android:textcolor="@android:color/black"
    android:ellipsize="marquee"
    android:focusable="true"
    android:marqueerepeatlimit="marquee_forever"
    android:focusableintouchmode="true"
    android:scrollhorizontally="true"
    android:text="这才是真正的文字跑马灯效果,文字移动速度,文字移动方向,文字移动的样式,动画等等……"
    android:background="#2fffffff" />
<button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="button" />
</linearlayout>

3. 在activity中声明

package com.example.tt;
import android.os.bundle;
import android.app.activity;
import android.view.menu;
public class mainactivity extends activity {
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android service组件使用技巧总结》、《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android开发入门与进阶教程》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网