修改EditText背景和光标

作者: 蓝不蓝编程 | 来源:发表于2019-01-24 19:53 被阅读2次

    背景

    安卓原生的EditText样式不太好看(光标和背景),不同的项目都需要定制。


    image.png

    解决方案

    1. 最优方案
      设置主题中colorAccent的颜色,即可改变EditText光标和背景颜色。
      如果不能改变application主题颜色,也可以单独针对指定的activity设定主题。
      样例:
      1). Manifest文件中application配置主题
    <application
                android:allowBackup="false"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
    

    2). styles.xml中修改colorAccent对应颜色即可。

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    

    效果图:


    image.png
    1. 次选方案
      如果不能修改主题,要单独定制某个EditText的光标和背景。可以通过设置android:background(背景)、android:textCursorDrawable(光标颜色)和android:textSelectHandle(焦点选择图标)
    <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2.定制光标、聚焦选择图标和背景(优选方案)"
                android:background="@drawable/edit_text_bg"
                android:textSelectHandle="@mipmap/edit_text_select"
                android:textCursorDrawable="@drawable/edit_text_cursor"/>
    

    效果图:同上

    参考代码完整demo:https://gitee.com/cxyzy1/editTextCursorColorDemo

    附录

    https://www.cnblogs.com/sparrowlhl/p/5780919.html
    https://blog.csdn.net/next_second/article/details/79182961

    安卓开发技术分享: https://www.jianshu.com/p/442339952f26

    相关文章

      网友评论

        本文标题:修改EditText背景和光标

        本文链接:https://www.haomeiwen.com/subject/mqsujqtx.html