按下向下箭头时隐藏键盘

时间:2015-05-13 07:24:28

标签: android

图片显示了我的应用程序的一部分,AutoCompleteTextView附带了适配器。 当用户在该视图中输入内容时,会显示自动填充建议。

我遇到的问题是:当显示建议并按下设备的向下箭头时,只有AutoCompleteTextView的建议被关闭,键盘保持打开状态,需要再次点击向下箭头消失。

我确实希望键盘上的建议在第一次点按向下箭头时消失。

我尝试覆盖onBackPressed但是在点击向下箭头时没有调用它,大概是因为它没有被考虑回来了#39}。

我怎么能这样做?

编辑:我知道如何以编程方式隐藏键盘,我想我的问题是检测到向下箭头'轻按。

enter image description here

3 个答案:

答案 0 :(得分:7)

尝试按如下方式覆盖AutoCompleteTextView中的onKeyPreIme()方法:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
        super.onKeyPreIme(keyCode, event);
        hideKeyboard()
        return true;
    }
    return super.onKeyPreIme(keyCode, event);
}

答案 1 :(得分:0)

您可以尝试这样的事情:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

    <!-- this is my image button -->
    <ImageButton
        android:id="@+id/imageButton"
        android:layout_gravity="bottom|end"
        android:layout_width="130dip"
        android:layout_height="65dip"
        android:scaleType="centerCrop"
        android:src="@drawable/save" />

</android.support.v4.widget.DrawerLayout>

我还没有尝试过代码,但我认为这是正确的方法。

答案 2 :(得分:-2)

InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);

您需要导入android.view.inputmethod.InputMethodManager;

相关问题