Android ActivityGroup与TabHost,键盘不显示。

3

我有一个TabHost,每个选项卡都有一个ActivityGroup。

当应用程序启动并且我点击EditText时,键盘会弹出。但是,当我启动子活动,然后返回主活动时,键盘不再弹出。

我启动子活动的代码如下:

Intent i = new Intent(this, ShowAddFoodToSelection.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

View view = ActivityGroupMeal.group.getLocalActivityManager().startActivity(DataParser.activityIDMeal, i).getDecorView();

ActivityGroupMeal.group.setContentView(view);

我的代码用于返回到主活动

ActivityGroupMeal.group.back();

在ActivityGroup中的后台代码:

public void back() {
        try {
            // if we set history.size() > 0 and we press back key on home
            // activity
            // and then on another activity we wont get back!
            if (history.size() > 1) {
                history.remove(history.size() - 1);
                // call the super.setContent view! so set the real view
                super.setContentView(history.get(history.size() - 1));
            } else {

            }
        } catch (Exception e) {
            if (history.size() >= 0)
                super.setContentView(history.get(0));
        }
    }

我在editText上设置了一个onClickListener,代码如下:
private void keyboardShow() {
        InputMethodManager inputManager = (InputMethodManager) ActivityGroupMeal.group.getSystemService(Context.INPUT_METHOD_SERVICE);

        boolean test = inputManager.showSoftInput(editTextSearch, InputMethodManager.SHOW_IMPLICIT);

        Toast.makeText(this, "show keyboard " + test, Toast.LENGTH_SHORT).show();
    }

第一次它返回 true,当我从子活动回来时它返回 false。
当我点击另一个选项卡,然后回到第一个选项卡,然后我再次点击 editText 时,它再次返回 true。
编辑:我有一个临时解决方法,我在 editText 上设置了一个 onClicklistener,然后在那里使用代码显示键盘。
InputMethodManager inputManager = (InputMethodManager) ActivityGroupMeal.group
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        // show keyboard , when it fails first switch tab and then try again
        if (!inputManager.showSoftInput(null, InputMethodManager.SHOW_FORCED)) {
            // switch from tab and back
            // the keyboard wont show if we dont do this
            ShowHomeTab parentActivity;
            parentActivity = (ShowHomeTab) this.getParent().getParent();
            parentActivity.goToTab(DataParser.activityIDTracking);
            parentActivity.goToTab(DataParser.activityIDShowFoodList);

            inputManager.showSoftInput(null, InputMethodManager.SHOW_FORCED);
        }

当我从子活动(childactivity)返回时,我必须先使用代码切换选项卡,然后键盘才会显示出来 =/。有人能解释一下吗?

我遇到了类似的问题,你有找到解决方案吗?请告诉我。 - kushaldsouza
我不确定。试试这个ActivityGroup - Praveenkumar
@k9ty 你有找到这个问题的解决方案吗??? - duggu
@Hemant 我已经找到了一个解决方法,我会尝试找出代码给你,但我不能保证能够很快找到,因为我已经有一段时间没有处理这个问题了。 - kushaldsouza
2个回答

4
在我使用Activity Group的应用程序中,我使用了以下代码来解决相同的问题。
YOUR_EDIT_TEXT.setOnEditorActionListener(new OnEditorActionListener() {

            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(searchName
                            .getApplicationWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });

它之前一直运行得很好。因此,请尝试这个代码片段。


尝试了一下。键盘在我的设备上显示,该设备具有Android 4.0.4固件版本,但在旧的固件版本(2.3及以下)的设备上仍然无法显示。 - kushaldsouza
在清单文件中的活动中添加android:windowSoftInputMode="stateVisible|adjustResize",然后检查。 - Pankaj Kumar
@PankajKumar 面临相同的问题并使用了您的代码,但没有任何反应?有没有解决这个问题的方法? - duggu

1
您可以尝试将requestFocus />元素添加到EditText的定义中,即:
<EditText android:id="@+id/edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" >
        <requestFocus />
</EditText>

如果这并没有帮助,您可以通过调用requestFocus()方法在back()方法中请求焦点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接