Android:AutoCompleteTextView as Collabsible ActionItem键盘未在Focus上显示

时间:2012-04-07 13:44:16

标签: android keyboard android-actionbar

我有一个AutoCompleteTextView作为Collabsible ActionItem,我希望键盘在展开和聚焦时显示。这是我在onCreateOptionsMenu()中的代码:

    menu.add("Search")
            .setIcon(R.drawable.ic_search)
            .setActionView(R.layout.collapsible_edittext)
            .setShowAsAction(
                    MenuItem.SHOW_AS_ACTION_ALWAYS
                            | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);


    final MenuItem searchMenuItem = menu.getItem(0);
    searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) item
                    .getActionView();
            autoCompleteTextView
                    .setOnFocusChangeListener(new OnFocusChangeListener() {

                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                            Log.d(TAG, "onFocusChange: " + hasFocus);
                            if (hasFocus) {
                                mInputManager.showSoftInput(v,
                                        InputMethodManager.SHOW_FORCED);
                            } else {
                                mInputManager.hideSoftInputFromWindow(
                                        v.getWindowToken(), 0);
                            }

                        }
                    });
            autoCompleteTextView.requestFocus();
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) item
                    .getActionView();

            autoCompleteTextView.clearFocus();
            autoCompleteTextView.setText("");
            return true;
        }
    });

当我第一次点击ActionItem时,没有显示键盘。折叠然后单击项目后,将显示键盘。但我希望在用户第一次点击操作项时显示键盘。 如果重要的话,我正在使用ActionBarSherlock。 那么为什么第一次扩展时键盘没有显示?有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您是否尝试过手动将焦点设置为编辑文本并在onOptionsItemSelected方法中手动显示键盘?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_SEARCH:
        // select edittext here
        // show keyboard here
        return true;

    }
    return super.onOptionsItemSelected(item);
}

编辑:我发现这段代码从这里显示键盘:

https://code.google.com/p/android-batavierenrace/source/browse/trunk/BataApp/src/com/ut/bataapp/activities/TeamsActivity.java

我在我的应用上测试了它并且有效...

public static void setKeyboardFocus(final EditText primaryTextField) {
                (new Handler()).postDelayed(new Runnable() {
                        public void run() {
                                primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
                                primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
                        }
                }, 100);
        }