Android 手機衛士--自定義控制項(獲取焦點的TextView)

来源:http://www.cnblogs.com/wuyudong/archive/2016/09/25/5906735.html
-Advertisement-
Play Games

本文地址:http://www.cnblogs.com/wuyudong/p/5906735.html,轉載請註明源地址。 本文將實現標題欄下麵的textview中的文字跑馬燈的效果,就是將一行文字水平迴圈滾動,效果如下: 實現代碼如下: 如果其他地方也需要這樣的跑馬燈效果,複製代碼比較麻煩。這裡使 ...


本文地址:http://www.cnblogs.com/wuyudong/p/5906735.html,轉載請註明源地址。

本文將實現標題欄下麵的textview中的文字跑馬燈的效果,就是將一行文字水平迴圈滾動,效果如下:

實現代碼如下:

    <!-- android:ellipsize="end"添加省略點的所在位置 -->
    <!-- 想讓文字出現跑馬燈效果,必須讓其獲取焦點 -->
    <!-- android:marqueeRepeatLimit="marquee_forever"一直滾動屬性 -->
    <!-- 自定義控制項達到滾動效果(其實就是重新原有的TextView,讓其一直能夠獲取焦點即可) -->
    <TextView 
        android:text="這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="5dp"
        android:textColor="#000"
        android:singleLine="true"
        />

如果其他地方也需要這樣的跑馬燈效果,複製代碼比較麻煩。這裡使用自定義控制項來實現滾動效果(其實就是重新原有的TextView,讓其一直能夠獲取焦點即可) 

新建一個包view,專門放自定義控制項文件

新建FocusTextView類

添加代碼:

package com.wuyudong.mobilesafe.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * @author wuyudong 
 * 能夠獲取焦點的自定義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 super.isFocused();
        return true;
    }
}

佈局代碼替換為:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        style="@style/TitleStyle"
        android:text="功能列表" />

    <!-- android:ellipsize="end"添加省略點的所在位置 -->
    <!-- 想讓文字出現跑馬燈效果,必須讓其獲取焦點 -->
    <!-- android:marqueeRepeatLimit="marquee_forever"一直滾動屬性 -->
    <!-- 自定義控制項達到滾動效果(其實就是重新原有的TextView,讓其一直能夠獲取焦點即可) -->
    <!--
      <TextView 
        android:text="這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="5dp"
        android:textColor="#000"
        android:singleLine="true"/> -->

    <com.wuyudong.mobilesafe.view.FocusTextView
        android:text="這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,這是一個跑馬燈,"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="5dp"
        android:textColor="#000"
        android:singleLine="true">
    </com.wuyudong.mobilesafe.view.FocusTextView>

</LinearLayout>

總結一下自定義控制項

自定義控制項編寫流程
創建一個預設就能獲取焦點的TextView

1、創建一個類繼承至TextView,FocusTextView

2、重寫其構造方法

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 super.isFocused();
        return true;
    }
}

3、將原有TextView上的isFocus方法預設修改為,能夠獲取焦點

    // 重寫獲取焦點的方法
    @Override
    public boolean isFocused() {
        // return super.isFocused();
        return true;
    }

4.使用過程

獲取當前類的全路徑名稱,作為xml中的標簽存在,其餘屬性的使用方式和TextView一致


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, ...
  • 本文是翻譯Function.apply and Function.call in JavaScript,希望對大家有所幫助 轉自“http://www.jb51.net/article/52416.htm” 第一次翻譯技術文章,見笑了! 翻譯原文: Function.apply and Functi ...
  • 1.定義網頁背景顏色 <body bgcolor="背景色"> 顏色可以用2種方式表示:1. 直接指定顏色名稱,如blue。2.使用十六進位數據表示如#RRGGBB,分別表示兩位十六進位數據. 2.設置背景圖片 <body background="圖片的地址"> 3.設置文字顏色 <body tex ...
  • 正常情況下 使用bootstrap 比原生代碼 寫響應式佈局更快 補充上一文《bootstrap的實際應用》: web端 必做相容性 適應各種瀏覽器 可能有幾套圖 經常使用雪碧圖 app端 有不同的屏幕尺寸 且必須進行自適應{ @media srceen 或者 bootstrap 或者 JS } 也 ...
  • 仿Bilibili iOS客戶端 練習啟動頁 源碼下載:http://code.662p.com/view/14534.html 首頁 分區 發現 我的 視頻信息 普通/直播 視頻播放 詳細說明:http://ios.662p.com/thread-3121-1-1.html ...
  • 禮物說仿寫(updating...) 源碼下載:http://code.662p.com/view/14507.html api: 禮物說 首頁精選 banner2: http://api.liwushuo.com/v2/secondary_banners?gender=1&generation=2 ...
  • 1 Data 執行時要操作的數據 在目標<data/>標簽中包含了以下幾種子元素,他們定義了url的匹配規則: android:scheme 匹配url中的首碼,除了“http”、“https”、“tel”...之外,我們可以定義自己的首碼 android:host 匹配url中的主機名部分,如“g ...
  • ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...