Android_去掉EditText控件周圍橙色高亮區域


先來看看默認的EditText控件效果:

布局就是一個Activity里就放了一個EditText控件,可以看到四周有橙色的高亮區域

處理后的效果:

 

接下來簡單描述下處理過程:

1,查看EditText這個類的源碼

public EditText(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.editTextStyle);
    }

在其構造方法里默認使用了系統定義的風格 com.android.internal.R.attr.editTextStyle

 

2,找到這個定義的屬性:

在android源碼的\frameworks\base\core\res\res\values路徑下找到attrs.xml文件,打開找到:

<!-- Default EditText style. -->
        <attr name="editTextStyle" format="reference" />

找到了源碼中引用的系統定義的風格,但這里啥都木有(一開始找到這,不知道format="reference"代表啥,在此感謝一個神奇的網站:stackoverflow)

接着找到這個<attr .... >的根節點

<!--根元素-->
<declare-styleable name="Theme">

<!-- n個attr及其他標簽-->

<!-- Default EditText style. -->
        <attr name="editTextStyle" format="reference" />

......

</.....>

根節點的name為"Theme",接着就是要找到name="Theme"的style

(也就是說format="reference"表示EditText控件所默認使用的com.android.internal.R.attr.editTextStyle資源定義在name="Theme"中,即后者被前者引用)

 

3, 找到name="Theme"的style:

attrs.xml所在路徑下有很多系統定義的資源,打開themes.xml, 在name="Theme"的style節點下可以看到:

<item name="editTextStyle">@android:style/Widget.EditText</item>

 

4,ok, 繼續,打開同路徑下styles.xml文件,內牛滿面,引用了半天,終於找到了

<style name="Widget.EditText">
        <item name="android:focusable">true</item>
        <item name="android:focusableInTouchMode">true</item>
        <item name="android:clickable">true</item>
        <item name="android:background">@android:drawable/edit_text</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="android:gravity">center_vertical</item>
    </style>

我們要去掉高亮區域,其實也就是換背景(后面就清楚的看到),與之相關的屬性自然是

<item name="android:background">@android:drawable/edit_text</item>

 

5,回到frameworks\base\core\res\res目錄,在drawable文件夾下找到edit_text.xml文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/textfield_default" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/textfield_disabled" />
    <item android:state_pressed="true" android:drawable="@drawable/textfield_pressed" />
    <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected" />
    <item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
    <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected" />
    <item android:drawable="@drawable/textfield_disabled" />
</selector>

這下應該很清楚了,在selector中定義了N多item表示N多狀態,看后面android:drawable="...."所引用的圖片資源,有好幾種

這里我先貼兩張系統內部使用的圖片:

@drawable/textfield_default

 

@drawable/textfield_selected

@drawable/textfield_disabled_selected

之所以這么小,因為是.9圖片,俗稱9妹

一目了然,所以上面說橙色高亮區域是有點不准確的,因為其實就是根據狀態來切換背景圖片而已

哪有什么高亮,橙色本來就是圖片的一部分(哎,語文太差, 描述不好)

 

這個東西挖到這,應該是挖到祖墳了,只要重寫一個背景xxx.xml供android:background屬性調用即可:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/textfield_default" />
    <item android:state_focused="true" android:drawable="@drawable/textfield_default" />
    <item android:drawable="@drawable/textfield_disabled" />
</selector>

注意上面自定義的selector用到的圖片,引用的是自己應用里的,因為android系統里定義的那幾張9妹圖片不是public的,咋不能直接調用

所以猥瑣了一下,把需要的圖片直接復制過來(復制到drawable-hdpi文件夾下,因為android系統也是放在這個文件夾下的,若放在drawable文件夾下,試了一下,上下會拉伸)

 

在你的布局文件里加上android:background="@drawable/xxx"即可

<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edittext1"
        android:background="@drawable/edittext"/>

 

到這里就可以實現上面的效果了

扯了這么多,其實都是查找系統本身定義的資源的過程,改的並不多

希望對需要的親們有所幫助


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM