0

我有一个 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。那么为什么在第一次扩展时没有显示键盘?有任何想法吗?

4

1 回答 1

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);
        }
于 2012-04-14T17:14:22.657 回答