我正在创建一个自定义的EditText类,因为我需要设置一些自定义字体;但是现在当我点击editText时,android键盘不再弹出......
这是我的班级:
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);
}
}
以及我如何在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" />
我认为聚焦事件是由我扩展EditText类...
的事实处理的任何提示?
答案 0 :(得分:71)
这是一个老问题,但如果有人关心,问题在于构造函数的实现:
public CustomFontEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
this.context = context;
}
您设置为0的最后一个参数(“defStyle”)应该是EditText的默认样式。如果你看看EditText类上的相同构造函数:
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
如您所见,应该使用EditText的默认样式,因此您的构造函数应如下所示:
public CustomFontEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
this.context = context;
}
答案 1 :(得分:1)
使用 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
}
}
答案 2 :(得分:0)
添加此
android:focusable="true"
答案 3 :(得分:0)
implements KeyListener on your custom EditText Class and override methods of KeyListener
答案 4 :(得分:0)
尝试在运行时编辑文本参考并调用请求焦点()
et.requestFocus()
并尝试
android:focusable="true"
答案 5 :(得分:-3)
editText.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
editText.setFocusableInTouchMode(true);
return false;
}
});