2017-09-19 7 views
-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> 
+1

チェックアウト、このリンク - https://stackoverflow.com/questions/11554078/set-textcursordrawable-programmatically – zephyr

+0

Robots-Light.ttfではなくRoboto_Light.ttfのようにフォント名を変更します。これは、フォントファイル名とアセットで「 - 」が使用できないためです。 – Ankita

+0

私は@zephyrというリンクを試しました。私のカスタムエディットテキストを変更しました。 –

答えて

0

FancyEditText、これを試してみてください::

public class FancyEditText extends EditText { 

    public FancyEditText(Context _Context) { 

     super(_Context); 
     init(null); 
     isInEditMode(); 
    } 

    public FancyEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     init(attrs); 
     isInEditMode(); 
    } 



    /** 
    * Reads font name attribute from attribute and sets font to TextView 
    * 
    * @param attrs 
    */ 
    private void init(AttributeSet attrs) { 

     if (!isInEditMode() && attrs != null) { 
      TypedArray a = getContext().obtainStyledAttributes(attrs, 
        R.styleable.Fonts); 
      Integer fontName = a.getInteger(R.styleable.Fonts_fontName, 
        0); 
      if (fontName != null) { 
       TypefaceManager.setTypeface(this, FontConstants 
         .getFontNameFromFontValue(fontName)); 
      } 
      a.recycle(); 
     } 
    } 

FontContants :::

public enum FontConstants { 

    SourceSansPro_Regular("fonts/SourceSansPro-Regular.otf", 5) 
    , SourceSansPro_Bold("fonts/SourceSansPro-Bold.otf", 6) 
    , SourceSansPro_Semibold("fonts/SourceSansPro-Semibold.otf", 7); 

    private String fontPathName; 
    private int fontValue; 

    private FontConstants(String fontPathName, int fontValue) { 
     this.fontPathName = fontPathName; 
     this.fontValue = fontValue; 
    } 

    /** 
    * @param value 
    * @return Returns font's file location existing in assets folder by comparing with Font value. Return's default font "ProximaNova-Regular.otf" if fontValue not exist . 
    */ 
    public static String getFontNameFromFontValue(int value) { 

     for (FontConstants font : FontConstants.values()) { 
      if (font.getFontValue() == value) 
       return font.getFontPathName(); 
     } 

     return SourceSansPro_Regular.getFontPathName(); 
    } 

    public int getFontValue() { 
     return fontValue; 
    } 

    public String getFontPathName() { 
     return fontPathName; 
    } 
} 

TypefaceManager :::解像度で

public class TypefaceManager { 

    /** The Typeface cache, keyed by their asset file name. */ 
     private static final Map<String, Typeface> mCache = new HashMap<String, Typeface>(); 

     /* 
     * Gets typeface from cache if already exist or creates from assets if not 
     * exist 
     */ 
     public static Typeface getTypeface(Context context, String fontPath) { 

      synchronized (mCache) { 
       if (!mCache.containsKey(fontPath)) { 
        Typeface t = Typeface.createFromAsset(context.getAssets(), 
          fontPath); 
        mCache.put(fontPath, t); 
       } 
      } 
      return mCache.get(fontPath); 

     } 

     /* 
     * sets type faces to textview 
     */ 
     public static boolean setTypeface(TextView target, String fontPath) { 

      Typeface tf = null; 
      try { 
       tf = getTypeface(target.getContext(), fontPath); 
      } catch (Exception e) { 
       return false; 
      } 
      target.setTypeface(tf); 
      return true; 

     } 

>値> attrs.xml :: activity_main.xml内部

<declare-styleable name="Fonts"> 
     <attr name="fontName" format="enum"> 
      <enum name="SourceSansPro_Regular" value="5" /> 
      <enum name="SourceSansPro_Bold" value="6" /> 
      <enum name="SourceSansPro_Semibold" value="7" /> 

    </attr> 
</declare-styleable> 

::

<com.sqlite.FancyEditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/btndone" 
     android:layout_marginTop="10dp" 
     android:textCursorDrawable="@null" 
     /> 
関連する問題