android - kotlin 在 Android 8 上隐藏软键盘
问题描述
我们正在尝试在开发过程中隐藏软键盘。翻译我们永远不想看到它。这是配置 Nexus 9 API 28 项目 SDK 26 项目是 Kotlin 这是清单的代码
<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 Question Question中的每一行代码
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)
解决方案
这应该有效:
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);
推荐阅读
- airflow - 将文件标记到 Airflow,使其不会尝试将它们作为 DAG 导入
- xml - 在 Perl 中使用 XML::Simple 将哈希转换为 XML 后内容丢失
- sql - 视图的每一列的定义
- node.js - 如何从用户名中获取用户?
- c - C API 中的 Z3 获取值
- django - Django Serializer 中的数据验证错误
- java - 如何在表中查找具有给定字符串输入的所有文档作为字符串列沙发库存储库列表中的字符串之一
- reactjs - 我怎样才能做出这个图表的反应?
- bash - Bash 逻辑语法?
- python - 最后 100 行的 SQLITE3 中的 SUM/AVG