我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
当前回答
调用此方法以隐藏软键盘
public void hideKeyBoard() {
View view1 = this.getCurrentFocus();
if(view!= null){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view1.getWindowToken(), 0);
}
}
其他回答
我的解决方案:
用活动构造它(视图是可选的),使用处理程序发布它(有一些延迟,例如100ms更好)。直接调用输入管理器有时不起作用。只有在工作时才能获得“活动”。我认为这很正常。
如果您可以在通话时获取根视图组或编辑视图,只需发送它即可获得更好的结果。
public class CloseSoftKeyboardRunnable implements Runnable
{
Activity activity;
View view; // for dialog will occur context getcurrentfocus == null. send a rootview to find currentfocus.
public CloseSoftKeyboardRunnable(Activity activity, View view)
{
this.activity = activity;
this.view = view;
}
public CloseSoftKeyboardRunnable(Activity activity)
{
this.activity = activity;
}
@Override
public void run() {
if (!activity.isFinishing())
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (view == null) {
view = activity.getCurrentFocus();
}
else
{
try {
view = ((ViewGroup)view).getFocusedChild();
}
catch ( Exception e) {}
}
Window window = activity.getWindow();
if (window != null)
{
if (view == null) {
try {
view = window.getDecorView();
view = ((ViewGroup)view).getFocusedChild();
}
catch ( Exception e) {}
}
if (view == null) {
view = window.getDecorView();
}
if (view != null) {
if (view instanceof EditText)
{
EditText edittext = ((EditText) view);
edittext.setText(edittext.getText().toString()); // reset edit text bug on some keyboards bug
edittext.clearFocus();
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
}
}
there are two ways to do so...
method 1:in manifest file
define the line **android:windowSoftInputMode="adjustPan|stateAlwaysHidden"** of code in your manifest.xml file as below...
<activity
android:name="packagename.youactivityname"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden" />
Method 2 : in Activity or Java class
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)`enter code here`;
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
它会work....@ASK
有很多答案,如果一切都不起作用,那么,这里有一个提示:),您可以制作EditText,
edittext.setAlpha(0f);
由于alpha方法,将看不到该编辑文本,现在使用上面的答案了解如何使用EditText显示/隐藏软键盘。
对于Xamarin.Android:
public void HideKeyboard()
{
var imm = activity.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>();
var view = activity.CurrentFocus ?? new View(activity);
imm.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.None);
}
如果有人感兴趣的话,我已经为Kotlin写了一个小扩展,但没有太多测试:
fun Fragment.hideKeyboard(context: Context = App.instance) {
val windowToken = view?.rootView?.windowToken
windowToken?.let {
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
}
App.instance是存储在Application中的静态“this”Application对象
更新:在某些情况下,windowToken为空。我增加了使用反射来检测键盘是否关闭的额外关闭方式
/**
* If no window token is found, keyboard is checked using reflection to know if keyboard visibility toggle is needed
*
* @param useReflection - whether to use reflection in case of no window token or not
*/
fun Fragment.hideKeyboard(context: Context = MainApp.instance, useReflection: Boolean = true) {
val windowToken = view?.rootView?.windowToken
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
windowToken?.let {
imm.hideSoftInputFromWindow(windowToken, 0)
} ?: run {
if (useReflection) {
try {
if (getKeyboardHeight(imm) > 0) {
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
}
} catch (exception: Exception) {
Timber.e(exception)
}
}
}
}
fun getKeyboardHeight(imm: InputMethodManager): Int = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight").invoke(imm) as Int