편집 텍스트 커서 색상 변경
프로그래밍 방식으로 EditText's
커서의 색상을 어떻게 변경할 수 있습니까?
Android 4.0 이상에서는 커서 색상 이 흰색입니다. 경우 EditText
의 배경도 흰색, 그것은 눈에 보이지 않는됩니다.
EditText 속성에는 속성이 있습니다. android:textCursorDrawable
이제 @null로 설정하십시오 .
android:textCursorDrawable="@null"
이제 EditText 커서가 EditText TextColor와 동일합니다.
Set EditText 커서 색상 에서 참조
이 문제를 해결할 방법을 찾았습니다. 가장 좋은 해결책은 아니지만 작동합니다.
불행히도 커서 색상에는 정적 색상 만 사용할 수 있습니다.
먼저 드로어 블에 검은 색 커서를 정의합니다.
<?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보기가 있습니다. 누군가가 내 솔루션을 개선하여 런타임에 커서를 변경할 수 있습니다.
여기에 @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>
styles.xml
AppCompat-v7을 사용하는 것은 어떻습니까?
<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>
나를 위해 작동합니다 (이 예는 단순합니다).
Jared 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) {
}
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>
따라서 여기에 최종 버전이 있습니다. XML이 필요하지 않고 @null로 작동하지만 프로그래밍 방식으로 작동합니다.
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set((TextView)yourEditText, 0);
} catch (Exception ignored) {
}
유일한 문제는 새로운 버전의 Android에서 언젠가 작동이 중지 될 수 있다는 것입니다.
추신 4.1.2에서 5.1로 테스트했습니다.
커서 색상으로 사용 되도록 android:textCursorDrawable
속성을 설정할 수 있습니다 .@null
android:textColor
속성 textCursorDrawable
은 API 레벨 12 이상에서 사용할 수 있습니다.
감사...
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>
use you can use code below
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@color/grey"
>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input you name"
android:padding="20dp"
android:textColor="@color/black"
android:textCursorDrawable="@null"
/>
<EditText
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input your age"
android:padding="20dp"
android:textCursorDrawable="@color/red"
android:textColor="@color/black"
/>
</LinearLayout>
Refecence from : change edittext cursor color android
참고URL : https://stackoverflow.com/questions/11649234/change-edittext-cursor-color
'Nice programing' 카테고리의 다른 글
ASP.NET MVC 전역 변수 (0) | 2020.10.28 |
---|---|
산란 용 matplotlib 컬러 바 (0) | 2020.10.28 |
Html Agility Pack은 클래스별로 모든 요소를 가져옵니다. (0) | 2020.10.28 |
TypeError 가져 오기 : __init __ () 필수 위치 인수 1 개 누락 : 항목이있는 자식 테이블 뒤에 부모 테이블을 추가하려고 할 때 'on_delete' (0) | 2020.10.28 |
네비게이션 컨트롤러의 투명한 모달 뷰 (0) | 2020.10.28 |