简体   繁体   English

Android:如何在按下按钮后保持软键盘可见

[英]Android: How to leave the softkeyboard visible after pressing the button

I have a screen with edittext and one button. 我有一个带有edittext和一个按钮的屏幕。 Edittext has properties Edittext具有属性

    android:focusable="true"
    android:focusedByDefault="true"
    android:focusableInTouchMode="true"

Button has the same properties with false values. Button具有与false值相同的属性。

On pressing the button there is a change edittext gravity. 按下按钮后,将更改edittext的重力。 The edittext does not lose focus when button clicked. 单击按钮时,edittext不会失去焦点。 But keyboard is hidding. 但是键盘是隐藏的。

How to make the keyboard not to hide and stay visible in this case? 在这种情况下,如何使键盘不隐藏并保持可见?

You can do this programatically . 您可以以编程方式执行此操作。 Call this when you click Button . 单击“ Button时调用此方法。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

or 要么

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

You can do this also with a tag like so: 您也可以使用如下标记来执行此操作:

 button.setTag(R.id.DONT_HIDE_KEYBOARD, "true");

and under values/ids.xml, add: 在values / ids.xml下,添加:

  <item name="DONT_HIDE_KEYBOARD" type="id"/>

try following.code,here focus is your EditText. 尝试following.code,这里的重点是您的EditText。

fun closeInput(focus: View?) {
            val inputMethodManager = mActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(focus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM