安卓开发安卓开发Android开发经验谈

安卓设置EditText获取焦点并弹出软键盘及手动关闭软键盘

2019-03-29  本文已影响2人  蓝不蓝编程
  1. 设置EditText获取焦点并弹出软键盘
private fun showSoftInput(activity: Activity, editText: EditText) {
    editText.requestFocus()
    activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
}
  1. 手动关闭软键盘
private fun hideSoftInput(activity: Activity) {
    var view = activity.currentFocus
    if (view == null) {
        view = View(activity)
    }
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0)
}

安卓开发技术分享: https://www.jianshu.com/p/442339952f26
更多技术总结好文,请关注:「程序园中猿」

上一篇 下一篇

猜你喜欢

热点阅读