问题
I'm trying to write a simple Android app in Kotlin. I have an EditText and a Button in my layout. After writing in the edit field and clicking on the Button, I want to hide the virtual keyboard.
There is a popular question Close/hide the Android Soft Keyboard about doing it in Java, but as far as I understand, there should be an alternative version for Kotlin. How should I do it?
回答1:
I think we can improve Viktor's answer a little. Based on it's always attached to a view, there will be context, if there is context then there is InputMethodManager
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
In this case the context automatically means the context of the view. What do you think?
回答2:
Use the following utility functions within your Activities, Fragments to hide the soft keyboard.
(*)Update for the latest Kotlin version
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
Old answer:
fun Fragment.hideKeyboard() {
activity.hideKeyboard(view)
}
fun Activity.hideKeyboard() {
hideKeyboard(if (currentFocus == null) View(this) else currentFocus)
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
This will close the keyboard regardless of your code either in dialog fragment and/or activity etc.
Usage in Activity/Fragment:
hideKeyboard()
回答3:
Simply override this method in your activity. It will automatically works in its child fragments as well.....
In JAVA
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return super.dispatchTouchEvent(ev);
}
In Kotlin
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (currentFocus != null) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
return super.dispatchTouchEvent(ev)
}
Vote up if it works for you.... Thanks.....
回答4:
Peter's solution solves neatly the problem by extending functionality of View class. Alternative approach could be to extend functionality of Activity class and thus bind operation of hiding keyboard with View's container rather than View itself.
fun Activity.hideKeyboard() {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(), 0);
}
回答5:
Make an object class named Utils:
object Utils {
fun hideSoftKeyBoard(context: Context, view: View) {
try {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
} catch (e: Exception) {
// TODO: handle exception
e.printStackTrace()
}
}
}
You can use this method in any class where you want to hide the soft input keyboard. I am using this in my BaseActivity
.
Here the view is any view that you use in your layout:
Utils.hideSoftKeyBoard(this@BaseActivity, view )
回答6:
You can use Anko to make life easier, so the line would be:
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
or maybe better to create extension function:
fun View.hideKeyboard(inputMethodManager: InputMethodManager) {
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
and call it like this:
view?.hideKeyboard(activity.inputMethodManager)
回答7:
I found the answer that worked for me here: http://programminget.blogspot.com/2017/08/how-to-close-android-soft-keyboard.html
val inputManager:InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.SHOW_FORCED)
回答8:
In your Activity or Fragment create a function as:
fun View.hideKeyboard() {
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(windowToken, 0)
}
suppose you have a button with an id your_button_id
in XML file related to this Activity or Fragment, so, on button click event:
your_button_id.setOnClickListener{
it.hideKeyboard()
}
回答9:
This works well with API 26.
val view: View = if (currentFocus == null) View(this) else currentFocus
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
回答10:
You can use from bellow code, I write bellow code in my fragment:
private val myLayout = ViewTreeObserver.OnGlobalLayoutListener {
yourTextView.isCursorVisible = KeyboardTool.isSoftKeyboardShown(myRelativeLayout.rootView)
}
Then in onViewCreated
of fragment
:
......
super.onViewCreated(view, savedInstanceState)
myRelativeLayout.viewTreeObserver.addOnGlobalLayoutListener(myLayout)
......
And in onDestroyView
use too:
override fun onDestroyView() {
super.onDestroyView()
myRelativeLayout.viewTreeObserver.removeOnGlobalLayoutListener(myLayout)
}
And:
object KeyboardTool {
fun isSoftKeyboardShown(rootView: View): Boolean {
val softKeyboardHeight = 100
val rect = Rect()
rootView.getWindowVisibleDisplayFrame(rect)
val dm = rootView.resources.displayMetrics
val heightDiff = rootView.bottom - rect.bottom
return heightDiff > softKeyboardHeight * dm.density
}
}
回答11:
Here is my solution in Kotlin for Fragment. Place it inside setOnClickListener
of the button.
val imm = context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
来源:https://stackoverflow.com/questions/41790357/close-hide-the-android-soft-keyboard-with-kotlin