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

私の解決策は、各フラグメントのすべての EditTexts に onTouchListener() を追加することでした-以下を参照してください。

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.
    }
}

フラグメント (またはアクティビティ) で、TabHost タイプを BugFixedTabHost に置き換えます。

最後に、レイアウト xml でも TabHost を使用すると仮定して、カスタム ビュー (完全なパッケージ名) に変更します。

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

これが@mattdondersで機能しなかった理由はわかりませんが、これが正しい方法です。また、すべての EditText にリスナーをアタッチするよりも安価です。ところで、mCurrentView.hasFocus() が False である理由はもうわかりましたか?

于 2014-01-11T20:48:11.713 に答える