You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 点击ExposedDropdownMenu下拉菜单时如何隐藏软键盘?

实现点击ExposedDropdownMenu自动隐藏软键盘方案

要实现该需求,核心逻辑是监听下拉触发控件(即内部的MaterialAutoCompleteTextView)的点击/焦点变化事件,主动调用系统方法隐藏软键盘即可,具体实现步骤如下:

步骤1:修改布局给内部输入控件添加ID

先给布局中TextInputLayout包裹的MaterialAutoCompleteTextView添加ID,方便后续在Fragment中获取实例:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/drop_down_menu"
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar"
    app:boxBackgroundColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="@+id/number_text_view"
    app:layout_constraintEnd_toStartOf="@+id/add_button"
    app:layout_constraintTop_toTopOf="@+id/number_text_view">

    <com.google.android.material.textfield.MaterialAutoCompleteTextView
        android:id="@+id/auto_complete_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:paddingStart="8dp"
        android:paddingEnd="8dp" />
</com.google.android.material.textfield.TextInputLayout>

步骤2:封装隐藏软键盘的通用方法

Kotlin 扩展函数实现

fun View.hideSoftKeyboard() {
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

Java 工具方法实现

public static void hideSoftKeyboard(View view) {
    InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

步骤3:在Fragment中绑定监听事件

在Fragment的onViewCreated生命周期方法中,获取控件实例后设置点击和焦点变化监听:

Kotlin 代码

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val autoCompleteTv = view.findViewById<MaterialAutoCompleteTextView>(R.id.auto_complete_tv)
    // 点击下拉时隐藏键盘
    autoCompleteTv.setOnClickListener {
        it.hideSoftKeyboard()
    }
    // 可选:控件获得焦点时也触发隐藏,覆盖更多触发场景
    autoCompleteTv.setOnFocusChangeListener { v, hasFocus ->
        if (hasFocus) {
            v.hideSoftKeyboard()
        }
    }
}

Java 代码

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    MaterialAutoCompleteTextView autoCompleteTv = view.findViewById(R.id.auto_complete_tv);
    // 点击下拉时隐藏键盘
    autoCompleteTv.setOnClickListener(v -> {
        hideSoftKeyboard(v);
    });
    // 可选:控件获得焦点时也触发隐藏,覆盖更多触发场景
    autoCompleteTv.setOnFocusChangeListener((v, hasFocus) -> {
        if (hasFocus) {
            hideSoftKeyboard(v);
        }
    });
}

补充说明

  • 你已经给MaterialAutoCompleteTextView设置了android:inputType="none",该控件本身不会主动唤起软键盘,所以只需在它被触发时关闭之前弹出的键盘即可。
  • 如果存在点击TextInputLayout非输入区域没触发隐藏的情况,可以给TextInputLayout也设置相同的点击监听逻辑。

内容的提问来源于stack exchange,提问作者Tom Darious

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 18:15:03