我正在使用像这样的自定义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
中设置光标?
答案 0 :(得分:0)
您可以扩展EditText而不是TextView
答案 1 :(得分:0)
试一下
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"
/>
答案 2 :(得分: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