2012-07-25 6 views
64

어떻게 변경할 수 있습니까 EditText's커서의 색 프로그래밍 방식으로 변경할 수 있습니까?edittext 커서 색을 변경하십시오

안드로이드 4.0 이상에서 커서 색상은 흰색입니다. EditText의 배경도 흰색 인 경우 보이지 않게됩니다. 당신의 EditText 속성에서

답변

99

는 속성 이제 @null 같은로 설정 android:textCursorDrawable

,

android:textCursorDrawable="@null"

그래서 지금 글고 커서가 글고 텍스트 색상과 동일하다. Set EditText cursor color

+5

xml이 아닌 프로그래밍 방식으로 프로그래밍해야합니다. – Adem

+0

유용한 정보 주셔서 감사합니다. – raju

+1

@Adem XML로 설정하면 프로그래밍 방식으로 텍스트 색상을 설정할 수 있습니다. – user363349

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> 

당김

에 검은 커서를 정의합니다.
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); 

지금 당신이 검은 커서가있는 글고보기가 있습니다
<?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> 

내가 런타임에 글고를 만들

, 내가 이것을 사용합니다. 커서를 런타임에 변경할 수 있도록 누군가가 내 솔루션을 향상시킬 수 있습니다.

+0

훌륭한 직업, 고마워요! 사용자 정의 EditText 구성 요소의 매력처럼 작동합니다. – Divers

14

@Adem이 게시 한 것보다 나은 해결책이라고 생각합니다.

자바 :

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:textColor의 사용됩니다 @nullandroid:textCursorDrawable 속성을 설정할 수 있습니다.

특성 textCursorDrawable

감사합니다 ...

7

무엇 styles.xml 사용 APPCOMPAT-V7에 대한 ... API 레벨 12 이상에서 사용할 수 있습니까?

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

나를 위해 일합니다 (이 예제는 단순합니다).

+0

Lollipop 이전 디바이스의 EditText 커서에이 액센트를 적용합니까? –

+0

아니요, 그러면 기본 시스템의 커서가 사용됩니다 (슬프게도). 좋은 답변은보다 완벽한 방식으로 게시되었습니다. – shkschneider

6

제라드 Rummler의 대답에 개선

자바 :

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) { 
} 

유일한 문제는 안드로이드의 새로운 버전으로 하루 작동하지 않을 수 있다는 것입니다.

PS 4.1.2에서 5.1로 테스트했습니다.

관련 문제