2016-06-15 58 views
0

我使用自定义的EDITTEXT这样EDITTEXT光标不可见的Android

public class DosisEdittextFont extends TextView { 
    public DosisEdittextFont(Context context) { 
     super(context); 
     init(context); 
    } 

    public DosisEdittextFont(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public DosisEdittextFont(Context context, AttributeSet attrs, 
     int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context) { 
     Typeface t = Typeface.createFromAsset(context.getAssets(), 
      "fonts/Dosis-Regular.otf"); 
     this.setTypeface(t); 
    } 
} 

但因为游标的不EditText显示。如何设置光标在自定义EditText

回答

0

您可能会延长,而不是TextView的

+0

你有一些代码来解释或文字 –

+1

卢卡斯Nicoletti你是对的..谢谢你的答复..它的工作.. – aj0822ArpitJoshi

+0

没问题的伙计,很高兴我发现问题在第一枪:p –

0

的EditText尝试这个

public class CustomEditText extends EditText { 

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     if(!isInEditMode()) 
     init(attrs); 
    } 

    public CustomEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     if(!isInEditMode()) 
     init(attrs); 

    } 

    public CustomEditText(Context context) { 
     super(context); 
     if(!isInEditMode()) 
     init(null); 
    } 

    private void init(AttributeSet attrs) 
    { 
     if (attrs!=null) 
     { 
      TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView); 
      String fontName = a.getString(R.styleable.CustomTextView_fontFamily); 

      if (fontName!=null) 
      { 
       Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName); 
       setTypeface(myTypeface); 
      } 
      a.recycle(); 
     } 
    } 
} 

要在您的布局

<CustomEditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/etEmailId" 
      android:hint="@string/email" 
      android:textColor="@color/input_text_color" 
      android:textColorHint="@color/input_text_color" 
      customfont:fontFamily="YOUR FONT NAME.ttf" 

      /> 
0
Reason for not appearance of cursor in edittext are many like your theme appearance  
i.e. theme style or edittext background and one is that you extending TextView, 
so try extending EditText in place of TextView as: 


import android.content.Context; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.EditText; 


public class CustomEditText extends EditText { 

private Context context; 
private AttributeSet attrs; 
private int defStyle; 

public CustomEditText(Context context) { 
    super(context); 
    this.context = context; 
    init(); 
} 

public CustomEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
    this.attrs = attrs; 
    init(); 
} 

public CustomEditText(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    this.context = context; 
    this.attrs = attrs; 
    this.defStyle = defStyle; 
    init(); 
} 

private void init() { 
    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf"); 
    this.setTypeface(font); 
} 

@Override 
public void setTypeface(Typeface tf, int style) { 
    tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf"); 
    super.setTypeface(tf, style); 
} 

@Override 
public void setTypeface(Typeface tf) { 
    tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf"); 
    super.setTypeface(tf); 
} 
} 

and in you layout xml 

<com.example.CustomEditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/edittext" 
     android:hint="text" 
     android:textColor="#000000" 
     android:textColorHint="#000000"/> 




So, try replacing textview with edittext , if still not resolve, this 
occurs due to your activity theme or your edittext background