键盘无缘无故重新打开

时间:2014-11-06 08:56:31

标签: java android android-layout android-edittext android-view

我的应用程序的主要活动在ABS中有一个classique搜索按钮

当我触摸它时,它会展开EditView并打开键盘

然后点击一些搜索词,有效,它会打开一个新的搜索活动而不完成主要活动

在您完成搜索后,您将触摸并返回主要活动

这里缩小了edittext并隐藏了键盘,但是它无缘无故地打开了!

在menu.xml文件中

<item
    android:id="@+id/action_search"
    android:actionLayout="@layout/view_menu_search"
    android:icon="@drawable/search"
    android:orderInCategory="100"
    android:showAsAction="always|collapseActionView"
    android:title="recherche"/>

view_menu_search.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cursorVisible="true"
    android:hint="@string/search_hint"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:textColor="@android:color/white"
    android:textColorHint="@android:color/white"
    android:textCursorDrawable="@null" />

在manifest.xml中

   <!-- Main -->
    <activity
        android:name=".ui.activity.ActivityMain"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustNothing|stateAlwaysHidden" >
    </activity>

在MainActivity.java中:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        getSupportMenuInflater().inflate(R.menu.menu_main, menu);
        mSearchEdit = (EditText) menu.findItem(R.id.action_search).getActionView();
        // search menuitem
        final MenuItem searchItem = menu.findItem(R.id.action_search);
        searchItem.setOnActionExpandListener(new OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Do something when collapsed
                closeKeyboardFocus();
                return true; // Return true to collapse action view
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                if (mSearchEdit.requestFocus()) {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                }
                openKeyboard();
                return true; // Return true to expand action view
            }
        });
        mSearchEdit.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    moveToSearchActivity(v.getText().toString(), true);
                    return true;
                }
                return false;
            }
        });
        return super.onPrepareOptionsMenu(menu);
    }

public void openKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

public void closeKeyboardFocus() {
    // Close keyboard
    InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    // check if no view has focus:
    View v = this.getCurrentFocus();
    if (v == null)
        return;
    inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

EDIT1:

现在我只是在onResume()中使用它来阻止键盘打开(但我不认为这是一个很好的解决方案)

// hide the keyboard simple trick
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

0 个答案:

没有答案