Kotlin在Android 8上如何隐藏软键盘

3

我们正在开发时尝试隐藏软键盘。翻译过来就是我们永远不想看到它。这里的配置是 Nexus 9 API 28 项目 SDK 26 项目使用 Kotlin。以下是 Manifest 的代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidstackoverflow.devconstraint"
android:windowSoftInputMode="stateAlwaysHidden">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"

    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:windowSoftInputMode="stateAlwaysHidden">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LayOutActivity"
        android:windowSoftInputMode="stateAlwaysHidden">
    </activity>
</application>

我们尝试了这个SO问题中的每一行代码 问题

LayOutActivity中的代码

open class LayOutActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_lay_out)
    this.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    val view = currentFocus
    //if (view != null) {
        //val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        //imm.hideSoftInputFromWindow(view.windowToken, 0)
    //}
    //val imm: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    //if (imm.isActive)
    //imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)

    //hideSoftKeyboard(view = null)


    //UIHelper.hideSoftKeyboard(activity = Activity())
        doALL()


}

//fun hideSoftKeyboard(view: View?) {
    //val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    //inputMethodManager.hideSoftInputFromWindow(view?.windowToken, 0)
//}
fun doALL(){
    //UIHelper.hideSoftKeyboard(activity = Activity())
    UIHelper.hideSoftKeyboard(view = null)
    UIHelper.hideKeyboard(this,etOne)
    etOne.setText("I have new Text")
}


object UIHelper {

    fun hideSoftKeyboard(activity: Activity?) {
        if (activity != null) {
            val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            if (activity.currentFocus != null && inputManager != null) {
                inputManager.hideSoftInputFromWindow(activity.currentFocus!!.windowToken, 0)
                inputManager.hideSoftInputFromInputMethod(activity.currentFocus!!.windowToken, 0)
            }
        }
    }

    fun hideSoftKeyboard(view: View?) {
        if (view != null) {
            val inputManager = view!!.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager?.hideSoftInputFromWindow(view!!.getWindowToken(), 0)
        }
    }

    fun hideKeyboard(activityContext: Context, editText: EditText) {
        editText.requestFocus()
        Handler().postDelayed({
            val inputMethodManager = activityContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.showSoftInput(editText, InputMethodManager.HIDE_IMPLICIT_ONLY)
        }, 250)
    }
}

我们只想知道如何保持软键盘隐藏。以前我们在LayOutActivity中使用了一行代码,它起作用了。这是Android 8还是Kotlin的一个新问题?以下是我们使用的那一行代码:

this.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
1个回答

1
这应该可以工作:

val inputManager: InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.SHOW_FORCED) // It can be done by show_forced too

Also, in AndroidManifest.xml:

android:windowSoftInputMode="stateHidden"

此句话的英译中文为:

另外,如果你在其中有EditText,请尝试使用:

editText.setShowSoftInputOnFocus(false);

请看这个链接: https://dev59.com/wmMk5IYBdhLWcg3wxAvy#49534949

还有这个链接: Android - 在 Android 8 上隐藏键盘


我们将您的代码发布到清单和创建活动中,但出现了错误“currentFocus不应为null”,那么我们该如何使currentFocus不为null? - Vector
因为我刚开始学习Kotlin,所以花了一些时间才弄清楚currentFocus不为空的错误。以下是你的代码需要更改的部分:将currentFocus?.windowToken替换为currentFocus!!.windowToken。请注意这个美妙的“?”符号。 - Vector
删除这行代码,然后检查一下怎么样?:setShowSoftInputOnFocus(false); - ʍѳђઽ૯ท
不需要使用setShowSoftInputOnFocus,但确实需要在清单文件中添加两行代码。问题出在EditText中输入文本的部分。 - Vector
1
经过许多痛苦的测试,似乎问题出在 Nexus 9 上的 API 28,您的代码和许多其他代码在这个模拟器上都无法正常工作。我在 Nexus 6 上进行了测试,API 28,您的代码运行良好。感谢您花费时间。如果有其他人遇到此问题,您需要在清单中添加代码以及在任何活动中添加代码,您不希望软输入键盘显示为模拟器测试,可以将 API 26 放在 Nexus 9 上或使用另一个平板电脑模拟器。 - Vector
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接