Android 8上的Kotlin隐藏软键盘

时间:2018-09-23 19:42:18

标签: android kotlin android-softkeyboard

我们正在尝试在开发过程中隐藏软键盘。翻译我们永远都不想看到它。这是配置Nexus 9 API 28 Project SDK 26项目是Kotlin这是清单的代码

Traceback (most recent call last):
File "/home/username/anaconda3/lib/python3.6/site- 
packages/mysql/connector/connection_cext.py", line 176, in 
_open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Bad handshake

我们已经尝试过此问题中的每一行代码 Question

LayOutActivity中的

代码

<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>

}

我们只想知道如何隐藏软键盘 我们之前使用过LayOutActivity中的一行代码,并且可以正常工作 这是Android 8或Kotlin的新问题吗? 这是我们行得通的

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)
    }
}

1 个答案:

答案 0 :(得分: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

此外,在AndroidManifest.xml中:

android:windowSoftInputMode="stateHidden"

此外,如果其中有EditText,请尝试使用:

editText.setShowSoftInputOnFocus(false);

查看以下内容:MongoDB

还有:https://stackoverflow.com/a/49534949/4409113