zoukankan      html  css  js  c++  java
  • 自定义获取焦点的TextView

    自定义控件编写流程
    创建一个默认就能获取焦点的TextView
    1.创建一个类继承至TextView,FocusTextView
    2.重写其构造方法

         //使用在通过java代码创建控件
            public FocusTextView(Context context) {
                super(context);
            }
            
            //由系统调用(带属性+上下文环境构造方法)
            public FocusTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
    
            //由系统调用(带属性+上下文环境构造方法+布局文件中定义样式文件构造方法)
            public FocusTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }

    3.将原有TextView上的isFocus方法默认修改为,能够获取焦点

         //重写获取焦点的方法,由系统调用,调用的时候默认就能获取焦点
            @Override
            public boolean isFocused() {
                return true;
            }

    4.使用过程
    获取当前类的全路径名称,作为xml中的标签存在,其余属性的使用方式和TextView一致

    public class FocusTextView extends TextView {
        //使用在通过java代码创建控件
        public FocusTextView(Context context) {
            super(context);
        }
        
        //由系统调用(带属性+上下文环境构造方法)
        public FocusTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        //由系统调用(带属性+上下文环境构造方法+布局文件中定义样式文件构造方法)
        public FocusTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        
        //重写获取焦点的方法,由系统调用,调用的时候默认就能获取焦点
        @Override
        public boolean isFocused() {
            return true;
        }
    }
    <!-- android:ellipsize="end"添加省略点的所在位置 -->
        <!-- 想让文字出现跑马灯效果,必须让其获取焦点 -->
        <!-- android:marqueeRepeatLimit="marquee_forever"一直滚动属性 -->
        <!-- 自定义控件达到滚动效果(其实就是重新原有的TextView,让其一直能够获取焦点即可) -->
        <!--
             <TextView 
            android:text="秋天秋天悄悄过去,留下小秘密,啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"
            android:textColor="#000"
            android:singleLine="true"
            android:padding="5dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        -->
    
        <com.itheima.mobilesafe74.view.FocusTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="5dp"
            android:singleLine="true"
            android:text="秋天秋天悄悄过去,留下小秘密,啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"
            android:textColor="#000" >
        </com.itheima.mobilesafe74.view.FocusTextView>
  • 相关阅读:
    hadoop上的C++程序开发
    some phrase for oral english
    python的安装,IDLE基本操作
    of这个变态
    一分钟先生: 程序员面试真经
    Cloud Tool 小探索
    【学习总结】数学-欧拉函数
    第三篇——第二部分——第一文 SQL Server镜像简单介绍
    NYOJ 915 +-字符串
    人类主动探索地外文明(METI)活动正在进行中
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/6221419.html
Copyright © 2011-2022 走看看