6

次のレイアウトを使用するアプリケーションがあります。

代替テキスト http://img15.imageshack.us/img15/238/screenshot003xbo.png

アプリが起動すると、フォーカスは最初の TextView にありますが、その中に文字を入力しようとすると、フォーカスは直接タブに移動します。この問題と戦っているのは私だけではないようで、おそらくこれは次のことに関連しています。

http://groups.google.com/group/android-developers/browse_thread/thread/435791bbd6c550a/8022183887f38f4f?lnk=gst&q=tabs+focus#8022183887f38f4f

とにかく、なぜそれが起こるのか分かりますか?そしてもちろん、回避策をいただければ幸いです。


質問のオーバーロードを避けるために、以下のコードを投稿します。

XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
          android:padding="5px"
          android:orientation="vertical" 
          android:id="@+id/task_edit_panel"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
           android:layout_weight="50" >

        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/title" 
                android:textStyle="bold" />

            <EditText android:id="@+id/title" 
              android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

        </LinearLayout>

        <TabHost android:id="@+id/edit_item_tab_host"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"> 

            <TabWidget android:layout_width="fill_parent"
                        android:layout_height="wrap_content" 
                        android:id="@android:id/tabs" /> 

            <FrameLayout
              android:id="@android:id/tabcontent"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:paddingTop="65px"> <!--  you need that if you don't want the tab content to overflow --> 

              <LinearLayout
                   android:id="@+id/edit_item_date_tab"
                   android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5px" > 

                    <TextView android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:text="date" 
                             android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                   android:id="@+id/edit_item_geocontext_tab"
                   android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5px" > 

                <TextView android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:text="lieu" 
                             android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                   android:id="@+id/edit_item_text_tab"
                   android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                     android:padding="5px"> 

                <EditText android:id="@+id/details" 
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:scrollbars="vertical" />

                </LinearLayout>

            </FrameLayout>

        </TabHost>

    </LinearLayout>

    <!-- Bottom pannel with "add item" button -->
    <LinearLayout 
          android:padding="5px"
          android:orientation="horizontal" 
          android:layout_weight="1"
          android:id="@+id/task_edit_panel"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="#E7E7E7" >

          <!--  Let the height set to fill_parent until we find a better way for the layout  -->
          <Button android:id="@+id/item_edit_ok_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="@string/ok"
                style="?android:attr/buttonStyleSmall"
                android:layout_weight="1" />

           <Button android:id="@+id/item_edit_cancel_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="@string/cancel"
                style="?android:attr/buttonStyleSmall"
                android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

そしてJavaコード:

    TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host);
    // don't forget this setup before adding tabs from a tabhost using a xml view or you'll get an nullpointer exception
    tab_host.setup(); 

    TabSpec ts1 = tab_host.newTabSpec("TAB_DATE");
    ts1.setIndicator(getString(R.string.when), getResources().getDrawable(R.drawable.ic_dialog_time));
    ts1.setContent(R.id.edit_item_date_tab);
    tab_host.addTab(ts1);

    TabSpec ts2 = tab_host.newTabSpec("TAB_GEO");
    ts2.setIndicator(getString(R.string.where),  getResources().getDrawable(R.drawable.ic_dialog_map));
    ts2.setContent(R.id.edit_item_geocontext_tab);
    tab_host.addTab(ts2);

    TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT");
    ts3.setIndicator(getString(R.string.what),  getResources().getDrawable(R.drawable.ic_menu_edit));
    ts3.setContent(R.id.edit_item_text_tab);
    tab_host.addTab(ts3);

    tab_host.setCurrentTab(0);
4

5 に答える 5

8

まあ、間違いなくバグ:http ://code.google.com/p/android/issues/detail?id=2516 。

これは高度なレイアウトの複雑なアプリにとって非常に厄介な問題であるため、次のリリースで修正されることを願っています。

于 2009-05-05T06:51:41.743 に答える
7

HoneyComb で同じ種類の問題に直面していましたが、非常に簡単な解決策を見つけました。ですから、誰かが私の経験を共有するのに役立つと思いました. タブがクリックされるたびに、テキストの編集にフォーカスが置かれ、実際にはハード キーボードで入力できますが、ソフト キーボードは決してポップアップしません。以下のコードでも、問題は解決しませんでした:

input1.requestFocusFromTouch();
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(input1, InputMethodManager.SHOW_FORCED);

解決策: タブを処理するメイン クラスで、Java コードの下に次のように記述します。

myTabHost.setOnTabChangedListener(new OnTabChangeListener(){    
    public void onTabChanged(String tabID) {    
        myTabHost.clearFocus(); 
    }   
}); 
于 2011-12-30T10:55:07.170 に答える
0

次の例は、この問題を修正する方法を示しています。

userNameEditText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                userNameEditText.requestFocusFromTouch();
                return false;
            }
        });
于 2013-02-27T16:00:56.880 に答える
0

すべての仕様を追加した後、JavaファイルのedittextにrequestFocusメソッドを追加します

于 2016-11-12T08:26:37.220 に答える