【问题标题】:EditText background base line color changing based on its focus in androidEditText背景基线颜色变化基于其在android中的焦点
【发布时间】:2014-10-28 23:38:47
【问题描述】:

我需要用下图所示的基线设计EditText,当它获得焦点时它会变成其他颜色。!

我正在使用以下内容。

 <EditText    
    android:id="@+id/mobilenumber" 
    android:drawableLeft="@drawable/mobile"
    android:drawablePadding="15dp" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:background="@drawable/edt_bg"
    android:singleLine="true"
    android:textColorHint="#FFFFFF"
    android:hint="@string/mobilenumber"
    android:layout_marginTop="20dp"
    android:layout_gravity="center"
    android:padding="5dp"
    android:imeOptions="actionNext"
    android:maxLength="10"
    android:textColor="#ffffff"
    android:textCursorDrawable="@null"

    />

所以,请指导我如何处理。

【问题讨论】:

  • 阅读 xml 中的 StateListDrawable 标签
  • 您能对此有一些清晰的认识吗?

标签: android colors background android-edittext


【解决方案1】:

尝试使用 Holo 颜色生成器生成自定义颜色。

Android Holo Colors

您可以创建选择器并将其设置为EditText的背景

res/drawable/edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

布局:

<EditText    
    android:id="@+id/mobilenumber"
    ....
    android:background="@drawable/edit_text" />

【讨论】:

  • 不错的答案。您的 XML 代码虽然缺少 标记。除此之外 - 谢谢兄弟!
【解决方案2】:

您可以为 EditText 添加主题

<style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@color/primary</item>
    <item name="colorControlActivated">@color/primary</item>
    <item name="colorControlHighlight">@color/primary</item>
</style>

你可以在 EditText 中使用

<EditText
    android:id="@+id/edtCode"
    android:layout_width="match_parent"
    android:layout_height="40dp"
   .......................
    android:theme="@style/EditTextTheme">

</EditText>

【讨论】:

  • 如果您想在选择字段时控制提示在字段上方上升时的颜色,请将相同的主题添加到包装您的 EditText 的 TextInputLayout。
  • 这会在 oppo 设备上崩溃。删除 style 中的 colorControl 参数可以修复它。试图找到更好的解决方案
  • 如何动态设置EditText的主题?
【解决方案3】:

在你的 editText 样式中使用它

<item name="backgroundTint">@color/focus_tint_list</item>

focus_tint_list.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:color="?attr/colorAccent"/>

   <item android:state_focused="false" android:color="#999999"/>

</selector>

【讨论】:

  • 你把文件focus_tint_list.xml放在哪里?
  • 在res中创建color资源目录,然后在color目录中创建focus_tint_list颜色资源
  • 如何将 Theme created 动态设置为 EditText ?我该怎么做才能动态设置该主题。
【解决方案4】:

我需要同样的东西,我通过以下方式解决了它:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_focused="true" android:state_window_focused="true">
   <shape>
     <stroke android:color="@color/blue_500" android:width="2dp" />
     <corners android:radius="45dp"/>
   </shape>
</item>
<item android:state_focused="false" android:state_window_focused="false">
  <shape>
    <stroke android:color="@color/grey_600" android:width="2dp"/>
    <corners android:radius="45dp"/>
  </shape>
</item>
</selector>

【讨论】:

  • 谢谢最佳答案
  • EditText的基线我们需要在哪里设置这个选择器文件?
【解决方案5】:

要在 EditText 处于焦点时更改其基线,您只需转到项目中的 colors.xml 文件并更改“colorAccent”值。

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="colorAccent">#DESIRED_COLOR</color>
</resources>`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2014-08-17
    相关资源
    最近更新 更多