回答
在你的EditText屬性外,還有一個屬性android:textCursorDrawable
現在它設置爲@null一樣,
android:textCursorDrawable="@null"
所以,現在你的EditText光標是與您的EditText TEXTCOLOR。
我找到了一種方法來解決這個問題。這不是最好的解決方案,但它有效。
不幸的是,我只能對遊標顏色使用靜態顏色。
首先,我定義在抽拉
<?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組件的魅力一樣工作。 – Divers 2014-03-26 11:44:01
這是,我認爲,比@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>
,您可以設置android:textCursorDrawable
屬性@null
,這將導致在使用android:textColor
作爲光標顏色。
屬性textCursorDrawable
是在API級別12和更高的可...
謝謝...
怎麼樣styles.xml
使用程序兼容性-V7?
<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>
適用於我(這個例子很簡單)。
這些重音符號是否適用於前棒棒糖設備上的EditText遊標? – 2015-05-20 22:15:26
不,它然後採用默認系統的光標(可悲)。已經發布了很好的答案,方式更完整。 – shkschneider 2015-05-21 08:05:40
上賈裏德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>
所以,這是最後的版本錫永 - 不需要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。
- 1. 如何更改光標顏色而不更改文字顏色?
- 2. 更改Android Studio中的編輯文本中的光標顏色和底線顏色
- 3. 更改可可中的文本光標(插入符)顏色?
- 4. 更改UITextField中光標的顏色
- 5. 更改光標的顏色android EditText
- 6. 更改Excel 2013中光標的顏色
- 7. 更改alertdialog中edittext的光標顏色
- 8. 通過styles.xml更改EditText光標顏色
- 9. R Studio:更改光標顏色
- 10. 如何更改FXForms中可編輯文本的顏色?
- 11. 根據焦點更改android編輯框中的文本顏色
- 12. 如何更改編輯文本行的顏色?
- 13. 更改標題UIActivityController的文本顏色
- 14. 更改標籤的文本顏色
- 15. UITextview隱藏光標/更改光標的顏色
- 16. css更改輸入光標/光標顏色
- 17. 編輯輸入值時更改顏色
- 18. 如何更改文本輸入Flex 4的文本光標顏色?
- 19. JQuery更改文本顏色
- 20. 更改TimePicker文本顏色
- 21. 更改文本顏色,$%
- 22. 更改文本顏色CSS3
- 23. 如何將文本標籤的顏色更改爲UITableViewCellStyleValue1顏色
- 24. matplotlib:更改標題和顏色條文本和刻度顏色
- 25. 如何根據文本的顏色更改CKEditor的文本編輯區域的背景顏色?
- 26. 在編輯UITableViewCell時更改標籤顏色
- 27. 如何更改swt樹編輯器中標籤的顏色
- 28. 更改與父文本顏色不同的子文本顏色?
- 29. Android編輯文本 - settextmethod光標問題
- 30. CONTENTEDITABLE文本編輯器,光標位置
我需要它編程,而不是基於xml的 – Adem 2012-07-25 12:24:43
感謝您提供有用的信息 – raju 2012-11-20 13:18:38
@Adem如果您在XML中設置此項,您可以將任何顏色以編程方式設置爲文本顏色。 – user363349 2013-04-23 15:02:57