5

我在 TabHost 中有一个片段,其中包含多个文本字段。虚拟键盘可以很好地使用 inputType 集输入文本,但硬件键盘(在 Droid、Droid 2 等上)不起作用。

从我开始在硬件键盘上打字的测试中,EditText 失去焦点,“打字”似乎去了应用程序的其他地方。我已经尝试了以下两种配置:

<EditText
     android:id="@+id/editTextPlusFat"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="0.15"
     android:background="@drawable/textfield_default_holo_light"
     android:digits="0123456789."
     android:ems="10"
     android:hint="@string/str_CalcHintFat"
     android:inputType="number" >

<EditText
     android:id="@+id/editTextPlusFat"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="0.15"
     android:background="@drawable/textfield_default_holo_light"
     android:ems="10"
     android:hint="@string/str_CalcHintFat"
     android:inputType="numberDecimal" >

有谁知道为什么会发生这种情况?谢谢你。

4

2 回答 2

6

我的解决方案是将 onTouchListener() 添加到每个片段中的所有 EditTexts - 见下文。

OnTouchListener foucsHandler = new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stub
        arg0.requestFocusFromTouch();
            return false;
    }
};

currentActivity.findViewById(R.id.editTextPlusServings).setOnTouchListener(foucsHandler);
currentActivity.findViewById(R.id.editTextPlusFoodName).setOnTouchListener(foucsHandler);
于 2013-01-09T13:33:21.333 回答
6

重复问题一样,更好的答案是通过覆盖 TabHost 的 onTouchModeChanged() 来消除焦点切换。

添加一个扩展 TabHost 的新类:

package net.lp.collectionista.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TabHost;

public class BugFixedTabHost extends TabHost {

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

    public BugFixedTabHost(Context context) {
        super(context);
    }

    @Override
    public void onTouchModeChanged(boolean isInTouchMode) {
        // leave it empty here. It looks that when you use hard keyboard,
        // this method would have be called and the focus will be taken.
    }
}

In your Fragment (or Activity) replace the TabHost type with BugFixedTabHost.

Finally, assuming you use TabHost in layout xmls too, change it to your custom view (full package name):

<net.lp.collectionista.ui.views.BugFixedTabHost
    android:id="@android:id/tabhost" ...

I'm not sure why this did not work for @mattdonders, but this is the right way to go. And it is cheaper than attaching listeners to every EditText. By the way, have we figured out yet why mCurrentView.hasFocus() is False or so?

于 2014-01-11T20:48:11.713 回答