[英]Custom EditText is not showing keyboard on focus
I am creating a custom EditText class because i need to set some custom fonts;我正在创建一个自定义 EditText 类,因为我需要设置一些自定义字体; However now when i click on the editText the android keyboard does not pop up anymore...
但是现在当我点击editText时,android键盘不再弹出......
here is my class:这是我的课:
package ro.gebs.captoom.utils.fonts;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import ro.gebs.captoom.R;
public class CustomFontEditText extends EditText {
private Context context;
public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode()) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontEditText,
defStyle, 0);
assert a != null;
int fontId = a.getInteger(R.styleable.CustomFontEditText_fontNameEdit, -1);
if (fontId == -1) {
throw new IllegalArgumentException("The font_name attribute is required and must refer "
+ "to a valid child.");
}
a.recycle();
initialize(fontId);
}
this.context = context;
}
public CustomFontEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
this.context = context;
}
public CustomFontEditText(Context context) {
super(context);
this.context = context;
}
@SuppressWarnings("ConstantConditions")
public void initialize(int fontId) {
Typeface tf = null;
switch (fontId) {
case 0:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Regular.ttf");
break;
case 1:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Bold.ttf");
break;
case 2:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Semibold.ttf");
break;
case 3:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-ExtraBold.ttf");
break;
}
setTypeface(tf);
}
}
and how i use it in XML:以及我如何在 XML 中使用它:
<ro.gebs.captoom.utils.fonts.CustomFontEditText
android:id="@+id/add_details_txt_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:hint="@string/type_here"
android:inputType="textPersonName"
custom:fontNameEdit="Regular" />
I thought the focusing events were handled by the fact that i extend the EditText class...我认为聚焦事件是由我扩展 EditText 类的事实处理的......
Any hints?任何提示?
It's an old question but if someone cares, the problem is on the implementation of the constructor:这是一个老问题,但如果有人关心,问题出在构造函数的实现上:
public CustomFontEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
this.context = context;
}
The last argument ("defStyle") which you set as 0, should be the default style for an EditText.您设置为 0 的最后一个参数 ("defStyle") 应该是 EditText 的默认样式。 If you take a look at the same constructor on the EditText class:
如果您查看 EditText 类上的相同构造函数:
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
As you can see, the default style for an EditText should be used, so your constructor should look like this:如您所见,应使用 EditText 的默认样式,因此您的构造函数应如下所示:
public CustomFontEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
this.context = context;
}
Make custom EditText with Kotlin to fix focus problem:使用Kotlin制作自定义 EditText 以解决焦点问题:
class MyEditText @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.editTextStyle) : AppCompatEditText(context, attrs, defStyleAttr) {
init {
doSomething(context)
}
private fun doSomething(context: Context) {
// TODO Set your custom font or whatever you need
}
}
添加这个
android:focusable="true"
implements KeyListener on your custom EditText Class and override methods of KeyListener
try to make reference for edit text at runtime and call request focus()尝试在运行时引用编辑文本并调用请求 focus()
et.requestFocus()
and try并尝试
android:focusable="true"
editText.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
editText.setFocusableInTouchMode(true);
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.