gpt4 book ai didi

android - 按回隐藏软键盘

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:54 26 4
gpt4 key购买 nike

我在 Activity 中有一个 EditText,我希望它处于 Activity 状态并且在我打开该 Activity 时打开软键盘。这是我的 EditTextxml:

<EditText
android:background="@null"
android:cursorVisible="true"
android:elegantTextHeight="true"
android:enabled="true"
android:focusable="true"
android:hint="Search"
android:id="@+id/editText11"
android:inputType="textNoSuggestions|textCapSentences"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:singleLine="true"
android:textColor="#000000"
android:textCursorDrawable="@null" />

并且我已将 android:windowSoftInputMode="stateVisible" 用于我拥有此 EditText 的 Activity 。

问题是,当我按一次 back 时,键盘不会隐藏(理想情况下它会在所有其他 EditText 中隐藏)并且当我按 back 再次关闭 Activity。在第一次 back 按下时,我没有在第二次 back 按下时收到对 onBackPressed() 的调用, 我愿意。为什么会出现这种行为,如何解决?

编辑我想要的是,如果键盘打开,按回键应该关闭键盘,如果键盘没有打开,则关闭 Activity 。

最佳答案

试试这个……

创建名为 Util 的类并放入此代码

public static void hideSoftKeyboard(Activity activity) {
final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
if (activity.getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}

并调用Activity的onBackPressed()

关于android - 按回隐藏软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com