2016-06-15 95 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