3

こんにちは、

みんな、すべてのフィールドのスタイル ファイル style.xml EditText を作成しました。フィールドがフォーカスを得たときに、スタイルを変更したいと思います。どうやって?

4

1 に答える 1

4

編集テキストの状態ごとにドローアブルを定義する必要があります。編集テキストのすべてのステート ドローアブルを定義する xml ファイルをドローアブル フォルダーに作成します。XML ファイルは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

指定したいステート ドローアブルごとに 1 つのアイテムを作成します。したがって、state_focused のドローアブルの場合は、次のようにします。

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/myFocusedEditText"
        android:state_focused="true"/>
    <item
        android:drawable="@drawable/myEnabledEditText"
        android:state_enabled="true"/>
</selector>

この XML ファイルに custom_edit_text.xml という名前を付け、drawables フォルダーにあることを確認します。次に、styles.xml で定義するだけです。

<style name="my_edit_text">
    <item name="android:drawable">@drawable/custom_edit_text</item>
</style>

幸運を!

于 2012-05-28T17:23:15.070 に答える