2012-07-25 156 views
64

如何更改EditText's光標的顏色以編程方式?更改編輯文本光標顏色

在android 4.0及以上版本中,光標顏色爲白色。並且如果EditText的背景也是白色的,則它變得不可見。

回答

99

在你的EditText屬性外,還有一個屬性android:textCursorDrawable

現在它設置爲@null一樣,

android:textCursorDrawable="@null"

所以,現在你的EditText光標是與您的EditText TEXTCOLOR。

參考從Set EditText cursor color

+5

我需要它編程,而不是基於xml的 – Adem 2012-07-25 12:24:43

+0

感謝您提供有用的信息 – raju 2012-11-20 13:18:38

+1

@Adem如果您在XML中設置此項,您可以將任何顏色以編程方式設置爲文本顏色。 – user363349 2013-04-23 15:02:57

26

我找到了一種方法來解決這個問題。這不是最好的解決方案,但它有效。

不幸的是,我只能對遊標顏色使用靜態顏色。

首先,我定義在抽拉

<?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視圖。也許有人可以改進我的解決方案,以便遊標可以在運行時更改。

+0

很棒的工作,謝謝!像定製EditText組件的魅力一樣工作。 – Divers 2014-03-26 11:44:01

14

這是,我認爲,比@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> 
1

,您可以設置android:textCursorDrawable屬性@null,這將導致在使用android:textColor作爲光標顏色。

屬性textCursorDrawable是在API級別12和更高的可...

謝謝...

7

怎麼樣styles.xml使用程序兼容性-V7?

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

適用於我(這個例子很簡單)。

+0

這些重音符號是否適用於前棒棒糖設備上的EditText遊標? – 2015-05-20 22:15:26

+0

不,它然後採用默認系統的光標(可悲)。已經發布了很好的答案,方式更完整。 – shkschneider 2015-05-21 08:05:40

6

上賈裏德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.cursor); 
} catch (Exception ignored) { 
} 

創建資源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" /> 

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> 
1

所以,這是最後的版本錫永 - 不需要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。