如何隐藏键盘。系统服务在 onCreate() 之前对 Activity 不可用
How to hide keyboard. System services not available to Activities before onCreate()
onCreate() 之前的 Activity 无法使用系统服务
我有对话框 class MyPersonalDialog(mContext: Context),这个对话框包含 EditText。
我通过在其中解析上下文来启动 class MyPersonalDialog。
val myPersonalDialog = MyPersonalDialog(这个)
然后我打电话给我
myPersonalDialog.showMyDialog
这个class:
class MyPersonalDialog(mContext: Context){
fun showMyDialog(){
val builder = AlertDialog.Builder(context)
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = layoutInflater.inflate(R.layout.dialog_edit_list_title, null)
view.renameListTitle.requestFocus()
val inputHelper = InputHelper(context)
inputHelper.showDialogKeyboard()
builder.setView(view)
builder.setNegativeButton(R.string.cancel, { dialogInterface: DialogInterface, i: Int ->
inputHelper.hideKeyboard(activity, view)
})
//some other code goes next
}
}
当用户按下 NegativeButton 按钮时,隐藏键盘开始工作
class InputHelper(val context: Context){
fun hideKeyboard(activity: Activity, view: View) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
但是出现这个错误:
java.lang.IllegalStateException: onCreate()
之前的 Activity 无法使用系统服务
我该如何解决这个问题?我不明白为什么会出现这个错误,因为 MyPersonalDialog class 是在 onCreate
之后启动的
找到解决方案:
class InputHelper(val context: Context){
fun showDialogKeyboard() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
fun hideKeyboard(view: View) {
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}}
将此添加到清单中的 Activity -
android:windowSoftInputMode="stateHiddenAlways"
fun hideKeyboard(activity: Activity) {
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Find the currently focused view, so we can grab the correct window token from it.
var view = activity.currentFocus
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
recyclerViewOfLists = RecyclerViewOfLists(cursor, this, MainActivity())
您不能仅通过调用构造函数来实例化活动 - 例如在这里您正在实例化一个新的 MainActivity
。此类活动不会针对您需要 activity 的任何内容进行初始化,例如访问系统服务或以其他方式用作 Context
.
不创建新实例,而是使用 this
传递对现有实例的引用,例如
recyclerViewOfLists = RecyclerViewOfLists(cursor, this, this)
尝试更换
fun hideKeyboard(activity: Activity, view: View) { val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) } }
在 InputHelper
onCreate() 之前的 Activity 无法使用系统服务
我有对话框 class MyPersonalDialog(mContext: Context),这个对话框包含 EditText。 我通过在其中解析上下文来启动 class MyPersonalDialog。 val myPersonalDialog = MyPersonalDialog(这个)
然后我打电话给我 myPersonalDialog.showMyDialog
这个class:
class MyPersonalDialog(mContext: Context){
fun showMyDialog(){
val builder = AlertDialog.Builder(context)
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = layoutInflater.inflate(R.layout.dialog_edit_list_title, null)
view.renameListTitle.requestFocus()
val inputHelper = InputHelper(context)
inputHelper.showDialogKeyboard()
builder.setView(view)
builder.setNegativeButton(R.string.cancel, { dialogInterface: DialogInterface, i: Int ->
inputHelper.hideKeyboard(activity, view)
})
//some other code goes next
}
}
当用户按下 NegativeButton 按钮时,隐藏键盘开始工作
class InputHelper(val context: Context){
fun hideKeyboard(activity: Activity, view: View) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
但是出现这个错误: java.lang.IllegalStateException: onCreate()
之前的 Activity 无法使用系统服务我该如何解决这个问题?我不明白为什么会出现这个错误,因为 MyPersonalDialog class 是在 onCreate
之后启动的找到解决方案:
class InputHelper(val context: Context){
fun showDialogKeyboard() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
fun hideKeyboard(view: View) {
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}}
将此添加到清单中的 Activity -
android:windowSoftInputMode="stateHiddenAlways"
fun hideKeyboard(activity: Activity) {
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Find the currently focused view, so we can grab the correct window token from it.
var view = activity.currentFocus
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
recyclerViewOfLists = RecyclerViewOfLists(cursor, this, MainActivity())
您不能仅通过调用构造函数来实例化活动 - 例如在这里您正在实例化一个新的 MainActivity
。此类活动不会针对您需要 activity 的任何内容进行初始化,例如访问系统服务或以其他方式用作 Context
.
不创建新实例,而是使用 this
传递对现有实例的引用,例如
recyclerViewOfLists = RecyclerViewOfLists(cursor, this, this)
尝试更换
fun hideKeyboard(activity: Activity, view: View) { val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) } }
在 InputHelper