繁体   English   中英

EditText光标在Android中不可见

[英]Edittext cursor not visible in android

我正在使用这样的自定义Edittext

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设置光标?

您可以扩展EditText而不是TextView

尝试这个

    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"

            />
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM