开发者社区> 问答> 正文

为什么Android TabHost会从TextView窃取焦点?

我有一个使用以下布局的应用程序:

替代文字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>

和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);

展开
收起
Puppet 2020-01-19 15:51:53 451 0
1 条回答
写回答
取消 提交回答
  • 好吧,绝对是一个错误:http : //code.google.com/p/android/issues/detail ? id=2516 。

    希望它将在下一个版本中得到解决,因为对于具有高级布局的复杂应用程序来说,这确实是一个令人讨厌的问题。

    2020-01-20 09:02:26
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载