Kotlin - 为 CustomFonts 打开软键盘时出现问题
Kotlin - Issue in opening Soft Keyboard for the CustomFonts
我在下面创建了 class 以在我的普通 EditText 中设置自定义字体或任何 otf 字体。
我正在使用 kotlin 并在下面创建 class 以推广使用 CustomEditText。
我在资产中添加了字体文件并在下面创建了 class 名为 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()
}
}
}
在此之前,我还创建了单独的 classes,即 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)
}
我在下面创建了 class 以在我的普通 EditText 中设置自定义字体或任何 otf 字体。 我正在使用 kotlin 并在下面创建 class 以推广使用 CustomEditText。
我在资产中添加了字体文件并在下面创建了 class 名为 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()
}
}
}
在此之前,我还创建了单独的 classes,即 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)
}