【问题标题】:change edittext cursor color更改edittext光标颜色
【发布时间】:2012-07-23 20:31:41
【问题描述】:

如何以编程方式更改EditText's光标颜色

在 android 4.0 及以上版本中,光标颜色为白色。如果EditTexts 背景也是白色的,它就会变得不可见。

【问题讨论】:

    标签: android user-interface


    【解决方案1】:

    在你的 EditText 属性中,有一个属性android:textCursorDrawable

    现在将其设置为 @null 就像,

    android:textCursorDrawable="@null"

    所以现在您的 EditText 光标与您的 EditText TextColor 相同。

    来自Set EditText cursor color的参考

    【讨论】:

    • 我需要它以编程方式,而不是基于 xml
    • @Adem 如果您在 XML 中设置它,您可以通过编程方式将任何颜色设置为文本颜色。
    • 这也不适用于提出的问题,即如何以编程方式设置它。
    • OP 希望通过代码而不是 xml 布局来实现,因此这不是正确的答案
    • @user370305 这使光标看起来很细,几乎不可见
    【解决方案2】:

    我找到了解决此问题的方法。这不是最好的解决方案,但它确实有效。

    不幸的是,我只能使用静态颜色作为光标颜色。

    首先,我在drawable中定义了一个黑色光标

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <solid android:color="#ff000000"/>
        <size android:width="1dp"/>
    </shape>
    

    接下来我在布局中定义一个示例 EditText。

    <?xml version="1.0" encoding="utf-8"?>
    <EditText xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textCursorDrawable="@drawable/blackpipe"
             >
        </EditText>
    

    当我想在运行时创建一个 EditText 时,我使用这个:

    AttributeSet editText30AttributeSet = null;
    int res = getResources().getIdentifier("edit30", "layout", getPackageName());//edit30 is EditText layout
    XmlPullParser parser = getResources().getXml(res);
    int state=0;
    do {
        try {
            state = parser.next();
        } catch (Exception e1) {
            e1.printStackTrace();
        }       
        if (state == XmlPullParser.START_TAG) {
            if (parser.getName().equals("EditText")) {
                editText30AttributeSet = Xml.asAttributeSet(parser);
                break;
            }
        }
    } while(state != XmlPullParser.END_DOCUMENT);
    EditText view = new EditText(getContext(),editText30AttributeSet);
    

    现在您有了一个带有黑色光标的 EditText 视图。也许有人可以改进我的解决方案,以便可以在运行时更改光标。

    【讨论】:

    • 干得好,谢谢!就像自定义 EditText 组件的魅力一样。
    【解决方案3】:

    我认为这是比@Adem 发布的更好的解决方案。

    Java:

    try {
        // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
        Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
        f.setAccessible(true);
        f.set(yourEditText, R.drawable.cursor);
    } catch (Exception ignored) {
    }
    

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <solid android:color="#ff000000" />
    
        <size android:width="1dp" />
    
    </shape>
    

    【讨论】:

      【解决方案4】:

      改进 Jared Rummler 的回答

      Java:

      try {
          // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
          Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
          f.setAccessible(true);
          f.set(yourEditText, R.drawable.custom_cursor);
      } catch (Exception ignored) {
      }
      

      在 res/drawable 文件夹中创建 custom_cursor.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" >
      
          <solid android:color="#ff000000" />
      
          <size android:width="1dp" />
      </shape>
      

      XML:

      <EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textCursorDrawable="@drawable/custom_cursor"
           >
      </EditText>
      

      【讨论】:

        【解决方案5】:

        styles.xml 使用 AppCompat-v7 怎么样?

        <item name="colorControlNormal">@color/accentColor</item>
        <item name="colorControlActivated">@color/accentColor</item>
        <item name="colorControlHighlight">@color/accentColor</item>
        

        对我有用(这个例子很简单)。

        【讨论】:

        • 这些重音符号是否适用于您在棒棒糖之前的设备上的 EditText 光标?
        • 不,然后它采用默认系统的光标(可悲)。从那以后,已经发布了很好的答案,而且更加完整。
        【解决方案6】:

        您可以将android:textCursorDrawable 属性设置为@null,这将导致使用android:textColor 作为光标颜色。

        属性 textCursorDrawable 在 API 级别 12 及更高级别中可用...

        谢谢...

        【讨论】:

          【解决方案7】:

          通过 AppTheme 设置

          <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. -->
              <item name="windowNoTitle">true</item>
              <item name="windowActionBar">false</item>
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
              <item name="android:spinnerStyle">@style/spinner_style</item>
              <!--Add This-->
              <item name="editTextStyle">@style/EdittextStyle</item>
              <item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
          </style>
          
          <!--New EditText Style-->
          <style name="EdittextStyle" parent="Widget.AppCompat.EditText">
              <item name="android:background">?attr/editTextBackground</item>
              <item name="android:textColor">?attr/editTextColor</item>
              <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
              <!--<item name="android:textCursorDrawable">@drawable/abc_text_cursor_material</item>-->
              <item name="colorControlNormal">@color/colorAccent</item>
              <item name="colorControlActivated">@color/colorAccent</item>
              <item name="colorControlHighlight">@color/colorAccent</item>
          
          </style>
          

          【讨论】:

            【解决方案8】:

            所以,这里是最终版本 - 不需要 XML 并且作为 @null 工作,但以编程方式:

            try {
                Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
                f.setAccessible(true);
                f.set((TextView)yourEditText, 0);
            } catch (Exception ignored) {
            }
            

            唯一的问题是它可能有一天会在某些新版本的 Android 上停止工作。

            PS 我在 4.1.2 到 5.1 上测试过。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-11-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-29
              • 2011-11-06
              • 1970-01-01
              相关资源
              最近更新 更多