-1
私は自分のアプリ全体にそれを使ってカスタム編集文を作成しました。edittextのカーソルは白で表示されるので、edittextの背景も白で表示されます。私たちはアンドロイド:textCursorDrawable = "@ null"をどこにでも実装してカーソルを見えるようにしなければなりません。編集文のカーソルをプログラム的に変更
私はTextCursorDrawableを私のカスタムEdittextに実装したいと思います。
CustomEditText
public class CustomEditText extends EditText {
private final static String NAME = "Roboto-Black";
private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(12);
EditText yourEditText;// = new EditText(context);
public CustomEditText(Context context) {
super(context);
if(!isInEditMode()){
yourEditText = new EditText(context);
init();
setCursorColor();
}
}
public CustomEditText (Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode()){
yourEditText = new EditText(context);
init();
setCursorColor();
}
}
private void setCursorColor() {
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}
}
public void init() {
Typeface typeface = sTypefaceCache.get(NAME);
if (typeface == null) {
typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
sTypefaceCache.put(NAME, typeface);
}
setTypeface(typeface);
}
}
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>
チェックアウト、このリンク - https://stackoverflow.com/questions/11554078/set-textcursordrawable-programmatically – zephyr
Robots-Light.ttfではなくRoboto_Light.ttfのようにフォント名を変更します。これは、フォントファイル名とアセットで「 - 」が使用できないためです。 – Ankita
私は@zephyrというリンクを試しました。私のカスタムエディットテキストを変更しました。 –