在片段中关闭软键盘

时间:2011-12-13 13:48:55

标签: android-fragments android-softkeyboard

我有一个包含SearchView的片段。当我在搜索框中单击时,会出现键盘。我输入一个搜索词并按“Enter”键盘消失,我得到了我的结果页面。当我在结果列表中选择一个项目时,我将被带回搜索片段并重新出现键盘。如何才能重新出现键盘?这是我的代码的一部分:

public class LocationFragment extends ListFragment implements OnItemLongClickListener, OnClickListener{
    // ===========================================================
    // Constants
    // ===========================================================
    private static final int BTN_ID = 0x5f000001;
    private static final String DEBUG_TAG = "Tablet/LocationFragment";
    // ===========================================================
    // Fields
    // ===========================================================
    private int pos;
    private int mPositionToDelete = 0;
    private FragmentListener listener;
    private Activity mActivity;

    // ===========================================================
    // Lifecycle
    // ===========================================================
    @Override
    public void onAttach(Activity activity) {
        mActivity = activity;
        super.onAttach(activity);
        try {
            listener = (FragmentListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement Fragment");
        }
    }
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
    }
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.locations_tile, null);
        SearchManager sm = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
        final SearchView sv = (SearchView)v.findViewById(R.id.search_view);
        sv.setSearchableInfo(sm.getSearchableInfo(getActivity().getComponentName()));
        sv.setSubmitButtonEnabled(true);
        consumeKeyUp(sv);
        return v;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setItemChecked(pos, true);
        lv.setSelector(R.xml.location_select_gradient);
        lv.setCacheColorHint(0x00000000);
        lv.setSelector(R.drawable.list_item_selector);
        lv.setOnItemLongClickListener(this);
        lv.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch(keyCode) {
                        case KeyEvent.KEYCODE_DPAD_DOWN:
//                          Log.i(DEBUG_TAG, "onkeyDown");
                            return !(lv.getSelectedItemPosition() < lv.getCount() - 1);
                        case KeyEvent.KEYCODE_DPAD_UP:
//                          Log.i(DEBUG_TAG, "onkey up");
                            if(lv.getSelectedItemPosition() == 0) {
//                              Log.i(DEBUG_TAG, " requesting for search focused");
                                return LocationFragment.this.getView().findViewById(R.id.search_view).requestFocus();
                            } else return false;
                        case KeyEvent.KEYCODE_DPAD_RIGHT:
                            Log.i(DEBUG_TAG, "KEYCODE_DPAD_RIGHT");
                            return true;
                    }
                    return false;
                }
                return false;
            }
        });

        ImageView closeBtn = (ImageView) getView().findViewById(R.id.locationsCloseButton);
        closeBtn.setOnClickListener(this);
        closeBtn.setFocusable(true);
        closeBtn.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    v.setOnKeyListener(new OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                                Log.i(DEBUG_TAG, "keypad Center");
                                return false;
                            }
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                return !(KeyEvent.KEYCODE_DPAD_DOWN == keyCode);
                            }
                            return false;
                        }
                    });
                } else {
                    v.setOnKeyListener(null);
                }

            }
        });
    }

2 个答案:

答案 0 :(得分:2)

您可以尝试清除列表中的搜索视图焦点点击回调,例如sv.clearFocus()

我使用MenuItem上的OnCreateOptionsMenu抓取搜索视图,在getActionView()中执行此操作。

顺便说一下,我也在onCreateOptionsMenu

的搜索视图中调用了以下内容
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemServiceContext.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);

老实说虽然我发现整个软输入键盘都是一种黑色艺术

答案 1 :(得分:0)

private SearchView mSearchView;

...

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);

    // Associate search able configuration with the SearchView
    android.app.SearchManager searchManager = (android.app.SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    mSearchView.setIconifiedByDefault(false);

    return super.onCreateOptionsMenu(menu);
}

private result() {
    mSearchView.clearFocus();
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
}

不要使用startSearch或onSearchRequested