gpt4 book ai didi

android - EditText 和 TabHost 不喜欢对方

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:17 24 4
gpt4 key购买 nike

我有一个包含 2 个选项卡的 EditText 和 TabHost 布局。安卓 1.6。

我在以下情况下使用硬件键盘。重现步骤:

  1. 当显示 Activity 时,EditText 获得焦点。

  2. 只要我按下任意键,EditText 就会失去焦点,第一个选项卡会获得焦点。

  3. 我再次单击 EditText 并开始输入。

  4. 除非我按下任何数字按钮,否则它会起作用。第一个选项卡再次获得焦点。

  5. 我使用轨迹球滚动回 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);
}

最佳答案

在深入挖掘 Android 源代码后,我发现了错误:TabHostonAttachedToWindow() 中注册了一个 OnTouchModeChangeListener,当离开触摸模式时(也就是当有人按下一把 key )。

我已经创建了一个解决方法:只需将它放在 src/info/staticfree/workarounds/TabPatchView.java 中,然后将此 View 添加到您使用选项卡的布局中。有关详细信息,请参阅下面的 Javadoc。

package info.staticfree.workarounds;

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

/**
* <p>
* This is a workaround for a bug in Android in which using tabs such that all the content of the
* layout isn't in the tabs causes the TabHost to steal focus from any views outside the tab
* content. This is most commonly found with EditTexts.
* </p>
* <p>
* To use, simply place this view in your @android:id/tablayout frameview:
* </p>
*
* <pre>
* <code>
* &lt;FrameLayout
* android:id="@android:id/tabcontent"
* android:layout_width="match_parent"
* android:layout_height="match_parent" >
*
* &lt;info.staticfree.workarounds.TabPatchView
* android:layout_width="0dip"
* android:layout_height="0dip" />
*
* [your actual content goes here]
* &lt;/FrameLayout>
*
* </pre>
*
* </code>
*
* @author <a href="mailto:steve@staticfree.info">Steve Pomeroy</a>
* @see <a href="http://code.google.com/p/android/issues/detail?id=2516">issue 2516</a>
*/
public class TabPatchView extends View {

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

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

public TabPatchView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
final TabHost tabhost = (TabHost) getRootView().findViewById(android.R.id.tabhost);
tabhost.getViewTreeObserver().removeOnTouchModeChangeListener(tabhost);
}
}

如果您的目标是 SDK 12+,则可以改用 OnAttachStateChangeListener。在 onCreate() 中,添加:

TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {

@Override
public void onViewDetachedFromWindow(View v) {}

@Override
public void onViewAttachedToWindow(View v) {
mTabHost.getViewTreeObserver().removeOnTouchModeChangeListener(mTabHost);
}
});

就是这样!

关于android - EditText 和 TabHost 不喜欢对方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3254406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com