我已经创建了下面的类,以在常规EditText中设置自定义字体或任何otf字体。我正在与kotlin一起工作,并在下面的类中创建了有关使用CustomEditText的通用说明。
我在资产中添加了字体文件,并在名为CustomEditText的类下创建:
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Typeface
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText
class CustomEditText : AppCompatEditText{
constructor(context: Context) : this(context, null) {
init(null)
}
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
init(attrs)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
init(attrs)
}
private fun init(attrs: AttributeSet?) {
CustomEditText(context, attrs, 0)
}
/**
* @param context:This is an abstract class whose implementation is provided by Android Operating System.
* @param attrs:A collection of attributes, as found associated with a tag in an XML document.
* @param defStyle:
*/
fun CustomEditText(
context: Context,
attrs: AttributeSet?,
defStyle: Int
) {
try {
val a: TypedArray =
context.obtainStyledAttributes(attrs, R.styleable.CustomEditText, defStyle, 0)
val customEnumValue: CustomEnumBrandonNew.CustomFontType =
CustomEnumBrandonNew.CustomFontType.fromId(
a.getInt(
R.styleable.CustomEditText_font_type,
0
)
)
a.recycle()
typeface = when (customEnumValue) {
CustomEnumBrandonNew.CustomFontType.BLACK ->
Typeface.createFromAsset(context.assets, "BrandonText-Black.otf") // BrandonTextBlack().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.BLACK_ITALIC ->
Typeface.createFromAsset(context.assets, "BrandonText-BlackItalic.otf") // BrandonTextBlackItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.BOLD ->
Typeface.createFromAsset(context.assets, "BrandonText-Bold.otf") // BrandonTextBold().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.BOLD_ITALIC ->
Typeface.createFromAsset(context.assets, "BrandonText-BoldItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.LIGHT ->
Typeface.createFromAsset(context.assets, "BrandonText-Light.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.LIGHT_ITALIC ->
Typeface.createFromAsset(context.assets, "BrandonText-LightItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.MEDIUM ->
Typeface.createFromAsset(context.assets, "BrandonText-Medium.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.MEDIUM_ITALIC ->
Typeface.createFromAsset(context.assets, "BrandonText-MediumItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.REGULAR ->
Typeface.createFromAsset(context.assets, "BrandonText-Regular.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.REGULAR_ITALIC ->
Typeface.createFromAsset(context.assets, "BrandonText-RegularItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.THIN ->
Typeface.createFromAsset(context.assets, "BrandonText-Thin.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
CustomEnumBrandonNew.CustomFontType.THIN_ITALIC ->
Typeface.createFromAsset(context.assets, "BrandonText-ThinItalic.otf") // BrandonTextBoldItalic().getInstance(context)?.getTypeFace()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
在此之前,我还创建了单独的类,即BrandonTextBlack,BrandonTextBlackItalic,BrandonTextBold等,如下所示:
class BrandonTextBold{
private var instance: BrandonTextBold? = null
private var typeface: Typeface? = null
fun getInstance(context: Context): BrandonTextBold? {
synchronized(BrandonTextBold::class.java) {
if (instance == null) {
typeface = Typeface.createFromAsset(context.resources.assets, "BrandonText-Bold.otf")
}
return instance
}
}
fun getTypeFace(): Typeface? {
return typeface
}
}
现在,在布局xml文件中,如下使用它:
<CustomEditText
android:id="@+id/edt_mobile_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dimen_40"
android:background="@drawable/drawable_round_button_transperent"
android:hint="@string/str_hint_enter_your_mobile_number"
android:imeOptions="actionDone"
android:inputType="number|phone"
android:maxLength="10"
android:textColorHint="@color/colorHintAndBorder"
android:maxLines="1"
android:paddingBottom="@dimen/dimen_8"
android:paddingTop="@dimen/dimen_8"
android:paddingRight="@dimen/dimen_15"
android:paddingLeft="@dimen/dimen_15"
android:textColor="@color/colorBlack"
android:textSize="@dimen/font_dimen_20"
app:font_type="regular" />
请注意,我已使用app:font_type =“ regular”。因此,我可以在我的edittext中使用普通的字体类型。完成。
但是,问题是当我按下或尝试在其中写东西时,软键盘没有打开。
可能是什么问题?
在所有构造函数中使用默认样式作为EditText样式,而不是0
发件人
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
init(attrs)
}
收件人
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, android.R.attr.editTextStyle) {
init(attrs)
}