EditText 和 TabHost 互不喜欢
我有一个包含 2 个选项卡的 EditText 和 TabHost 布局。安卓1.6。
我在以下情况下使用硬件键盘。重现步骤:
当显示活动时,EditText 获得焦点。
一旦我按任意键,EditText 就会失去焦点,而第一个选项卡会获得焦点。
我再次单击 EditText 并开始输入。
除非我按任何数字按钮,否则它会起作用。第一个选项卡再次获得焦点。
我用轨迹球滚动回 EditText。我现在可以输入任何内容。
在步骤 2,3 中对聚焦的 EditText 使用左/右轨迹球也会使 EditText 失去焦点。
这很奇怪。遇到这种情况该如何处理呢?
main.xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText android:id="@+id/textfield" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:maxLines="1"
android:lines="1" android:hint="Search" />
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout android:id="@+id/tab1"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/tab2"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</FrameLayout>
<TabWidget android:id="@android:id/tabs"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="bottom" />
</LinearLayout>
</TabHost>
</LinearLayout>
Activity 类:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = (TabHost) this.findViewById(android.R.id.tabhost);
tabs.setup();
Button vBtn;
TabSpec tspec1 = tabs.newTabSpec("label one");
vBtn = new Button(this);
vBtn.setText("1");
tspec1.setIndicator(vBtn);
tspec1.setContent(R.id.tab1);
TabSpec tspec2 = tabs.newTabSpec("label two");
vBtn = new Button(this);
vBtn.setText("2");
tspec2.setIndicator(vBtn);
tspec2.setContent(R.id.tab1);
tabs.addTab(tspec1);
tabs.addTab(tspec2);
}
I have a layout with EditText and TabHost containing 2 tabs. Android 1.6.
I use hardware keyboard in following case. Steps to reproduce:
When activity is displayed the EditText gains focus.
As soon as I press any key the EditText loses focus and first tab gains it.
I click on the EditText again and start typing.
It works unless I press any numeric button. The first tab gains the focus again.
I scroll back to the EditText with track ball. I can type anything now.
Using track ball left/right on focused EditText in steps 2,3 also makes the EditText lose its focus.
This is very weird. How to handle with this?
main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText android:id="@+id/textfield" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:maxLines="1"
android:lines="1" android:hint="Search" />
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout android:id="@+id/tab1"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/tab2"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</FrameLayout>
<TabWidget android:id="@android:id/tabs"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="bottom" />
</LinearLayout>
</TabHost>
</LinearLayout>
Activity class:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = (TabHost) this.findViewById(android.R.id.tabhost);
tabs.setup();
Button vBtn;
TabSpec tspec1 = tabs.newTabSpec("label one");
vBtn = new Button(this);
vBtn.setText("1");
tspec1.setIndicator(vBtn);
tspec1.setContent(R.id.tab1);
TabSpec tspec2 = tabs.newTabSpec("label two");
vBtn = new Button(this);
vBtn.setText("2");
tspec2.setIndicator(vBtn);
tspec2.setContent(R.id.tab1);
tabs.addTab(tspec1);
tabs.addTab(tspec2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在深入研究 Android 源代码后,我发现了以下错误:
TabHost
在onAttachedToWindow()
中注册了一个 OnTouchModeChangeListener,该监听器在离开触摸模式时(也称为当有人按下某个键时)会窃取焦点。我创建了一个解决方法:只需将其放在 src/info/staticfree/workarounds/TabPatchView.java 中,然后将此视图添加到使用选项卡的布局中。有关更多详细信息,请参阅下面的 Javadoc。
如果您的目标是 SDK 12+,则可以改用
OnAttachStateChangeListener
。在onCreate()
中,添加:就这样!
After digging extensively through the Android source, I found the bug:
TabHost
registers an OnTouchModeChangeListener inonAttachedToWindow()
that steals focus when leaving touch mode (aka when someone presses a key).I've created a workaround: just place this in
src/info/staticfree/workarounds/TabPatchView.java
and add this view to your layout where you use tabs. See the Javadoc below for more details.If you're targeting SDK 12+, you can use
OnAttachStateChangeListener
instead. InonCreate()
, add:And that's it!
您确实不想在 XML 布局文件中使用 TabHost 和 Tabwidget 来设置选项卡。
我通过扩展 TabActivity 的类中的代码完成了这一切,对于每个选项卡,我只是将一个 Activity 与其自己的 XML 布局文件关联起来,该文件负责该特定选项卡的 UI 布局。
下面是 TabActivity 类,也是负责 TAB 标签外观的 background_selectors XML 示例。
以下是first_tab_background_selectors XML 文件:
selected_tab_icon 和unselected_tab_icons 是.PNG 图像文件,遵循Android 开发人员指南中指示的大小、分辨率和其他图形准则。
You really don't want to use TabHost and Tabwidget in the XML layout file to setup your tabs.
I did it all through code in a class that extends TabActivity, and for each tab, I just associated an Activity with its own XML Layout file that took care of the UI layout for that specific tab.
Here is the TabActivity class, and also an example of the background_selectors XML that takes care of the appearance of the TAB Label.
Here is the first_tab_background_selectors XML file:
The selected_tab_icon and the unselected_tab_icons are .PNG image files, following the size, resolution, and other graphics guidelines as instructed in the Android Developers Guide.