[英]Custom EditText is not showing keyboard on focus
我正在創建一個自定義 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 類的事實處理的......
任何提示?
這是一個老問題,但如果有人關心,問題出在構造函數的實現上:
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;
}
使用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
嘗試在運行時引用編輯文本並調用請求 focus()
et.requestFocus()
並嘗試
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.