【问题标题】:android:stateFocused = "false" not working in EditTextandroid:stateFocused = \"false\" 在 EditText 中不起作用
【发布时间】:2022-10-07 21:33:11
【问题描述】:

我的项目中有 2 个编辑文本,即 usernameFieldpasswordFieldusernameField 有一个 android:drawableStart="@drawable/custom_username_field",它将 EditText 的开始设置为 custom_username_field,其中 custom_username_field 具有以下 xml 代码

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false"
        android:drawable="@drawable/ic_user_icon"/>

    <item android:state_focused="true"
        android:drawable="@drawable/ic_user_icon_focused"/>

</selector>

它非常简单。如果它被聚焦,那么它将使用@drawable/ic_user_icon_focused,如果不是,那么它将使用@drawable/ic_user_icon

但问题是当我启动应用程序时,它使用用户名字段的焦点图标而不是默认图标,如果我将焦点更改为passwordField,那么usernameField 的图标将更改为ic_user_iconpasswordField,与usernameField 具有相同的代码,更改为焦点,但如果我不关注passwordField,它仍然有焦点图标。

我知道这可能不是在堆栈溢出上发布的有效问题,但我似乎无法找到解决方案。

【问题讨论】:

    标签: android


    【解决方案1】:

    如下例所示,将标签 android:focusableInTouchMode="true"android:focusable="true" 添加到父布局(例如 LinearLayout 或 ConstraintLayout)中,将解决问题。

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_width="0px" 
        android:layout_height="0px"/>
    
    <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
    to prevent the dummy from receiving focus again -->
    <AutoCompleteTextView android:id="@+id/autotext"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:nextFocusUp="@id/autotext" 
        android:nextFocusLeft="@id/autotext"/>
    

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 2016-03-02
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多