在HTC Desire S上,软键盘没有弹出EditText焦点

时间:2013-04-09 05:08:25

标签: android android-layout android-edittext android-keypad

我有一个带imeOptions="actionSearch"的EditText。现在,当我点击EditText时,键盘不会弹出一些HTC设备,如HTC Desire S& HTC Incredible。 EditText xml如下:

<com.myapp.utilities.font.DroidSansEditText
                    android:id="@+id/txtSearchByName"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.60"
                    android:background="@drawable/edittext"
                    android:ellipsize="end"
                    android:gravity="left|center_vertical"
                    android:hint="@string/provider_practice"
                    android:imeOptions="actionSearch"
                    android:inputType="textCapWords"
                    android:paddingLeft="10dp"
                    android:paddingRight="5dp"
                    android:singleLine="true"
                    android:tag="byName"
                    android:textColor="#a19d93"
                    android:textSize="15.5sp" />

DroidSansEditText是具有EditText的自定义droid-sans font。 DroidSansEditText如下:

public class DroidSansEditText extends EditText {
    public DroidSansEditTextView(Context context) {
        super(context);     
    }

    public DroidSansEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DroidSansEditTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setTypeface(Typeface tf, int style) {
        if (!isInEditMode()) { //For Graphical layout of xml in eclipse 
            // here Bold style is handled. Same way we can handle italic style
            if (style == 1) { // if bold (Style Constant, normal = 0 | bold = 1
                                // | italic = 2)
                tf = Typeface.createFromAsset(getContext()
                        .getApplicationContext().getAssets(),
                        "DroidSans-Bold.ttf");
            } else {
                tf = Typeface.createFromAsset(getContext()
                        .getApplicationContext().getAssets(), "DroidSans.ttf");
            }
            super.setTypeface(tf, 0);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

也许是因为如果这个EditText处于编辑模式,实际上你没有设置字体?

我的意思是,你为什么需要这个:

 if (!isInEditMode())

如果处于编辑模式,您想要设置另一种字体,请编写 else 语句并在其中设置另一种字体。

因为现在 - 如果TextView处于编辑模式,则不会调用super.setTypeface();所以你只需覆盖方法,不做任何事情。请尝试让我知道

答案 1 :(得分:0)

有一种方法可以在单击编辑文本时显示和隐藏键盘以键入内容。找到隐含显示键盘的行下方。

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

编辑文本是您正在使用的编辑文本。当屏幕上的其他位置触摸时隐藏它只需执行此操作。获取屏幕的最顶层布局并在顶部布局的触摸方法上实现此操作,以便当被触摸到编辑文本键盘以外的其他地方时会被解雇。

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
相关问题