Close/hide MvxFragment 中的 Android 软键盘
Close/hide the Android Soft Keyboard in MvxFragment
我使用 xamarin + mvvmcross 创建 android 应用程序。我的 MvxFragment 中有一个 MvxAutoCompleteTextView。在 MvxAutoCompleteTextView 中写入并单击其他控件后,我想隐藏虚拟键盘。我用这个代码
public class MyFragment : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.frMy, null);
var autoComplete = view.FindViewById<MvxAutoCompleteTextView>(Resource.Id.acMy);
InputMethodManager inputManager = (InputMethodManager)inflater.Context.GetSystemService(Context.InputMethodService);
inputManager.HideSoftInputFromWindow(autoComplete.WindowToken, HideSoftInputFlags.None);
return view;
}
}
但这行不通。如何隐藏键盘?
试试我的功能:
public static void Close_AndroidKeyboard(Activity context){
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
抱歉,我在 android 工作室工作,请告诉我我是否对您有所帮助,以及好的编程!
您可以隐藏软键盘,将焦点放在非 "keyboard launcher" 控件的对象上,例如,自动完成控件的父容器。
parentContainer = FindViewById<LinearLayout>(Resource.Id.parentContainer);
parentContainer.RequestFocus();
假设您的父容器是一个 LinearLayout,您应该允许它通过以下 2 个属性获得焦点:
<LinearLayout
android:id="@+id/parentContainer"
android:focusable="true"
android:focusableInTouchMode="true">
试试这个:
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, 0);
HideSoftInputFromWindow
中的值 0
是常量 Android.Views.InputMethods.HideSoftInputFlags.None
,因此您可以使用等效语法:
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
我从这里得到了 java 中的答案:
这是 C# 版本(已测试并且可以工作):
public override bool DispatchTouchEvent(MotionEvent ev)
{
if (ev.Action == MotionEventActions.Down)
{
View v = CurrentFocus;
if (v.GetType() == typeof(EditText))
{
Rect outRect = new Rect();
v.GetGlobalVisibleRect(outRect);
if (!outRect.Contains((int)ev.RawX, (int)ev.RawY))
{
v.ClearFocus();
InputMethodManager imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(v.WindowToken, 0);
}
}
}
return base.DispatchTouchEvent(ev);
}
如果您单击任何非 EditText 的内容,这段代码将隐藏软键盘。
您只需将其粘贴到您的 activity class(例如您的 loginActivity)
我找到的最简单的解决方案是简单地在具有焦点的控件上调用 Unfocus()
方法(例如 Entry
或 Editor
)。如果您想直接在 XAML 中指定函数,可以将此调用嵌入到 UnfocusOnClicked 行为中。
我使用 xamarin + mvvmcross 创建 android 应用程序。我的 MvxFragment 中有一个 MvxAutoCompleteTextView。在 MvxAutoCompleteTextView 中写入并单击其他控件后,我想隐藏虚拟键盘。我用这个代码
public class MyFragment : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.frMy, null);
var autoComplete = view.FindViewById<MvxAutoCompleteTextView>(Resource.Id.acMy);
InputMethodManager inputManager = (InputMethodManager)inflater.Context.GetSystemService(Context.InputMethodService);
inputManager.HideSoftInputFromWindow(autoComplete.WindowToken, HideSoftInputFlags.None);
return view;
}
}
但这行不通。如何隐藏键盘?
试试我的功能:
public static void Close_AndroidKeyboard(Activity context){
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
抱歉,我在 android 工作室工作,请告诉我我是否对您有所帮助,以及好的编程!
您可以隐藏软键盘,将焦点放在非 "keyboard launcher" 控件的对象上,例如,自动完成控件的父容器。
parentContainer = FindViewById<LinearLayout>(Resource.Id.parentContainer);
parentContainer.RequestFocus();
假设您的父容器是一个 LinearLayout,您应该允许它通过以下 2 个属性获得焦点:
<LinearLayout
android:id="@+id/parentContainer"
android:focusable="true"
android:focusableInTouchMode="true">
试试这个:
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, 0);
HideSoftInputFromWindow
中的值 0
是常量 Android.Views.InputMethods.HideSoftInputFlags.None
,因此您可以使用等效语法:
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
我从这里得到了 java 中的答案:
这是 C# 版本(已测试并且可以工作):
public override bool DispatchTouchEvent(MotionEvent ev)
{
if (ev.Action == MotionEventActions.Down)
{
View v = CurrentFocus;
if (v.GetType() == typeof(EditText))
{
Rect outRect = new Rect();
v.GetGlobalVisibleRect(outRect);
if (!outRect.Contains((int)ev.RawX, (int)ev.RawY))
{
v.ClearFocus();
InputMethodManager imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(v.WindowToken, 0);
}
}
}
return base.DispatchTouchEvent(ev);
}
如果您单击任何非 EditText 的内容,这段代码将隐藏软键盘。 您只需将其粘贴到您的 activity class(例如您的 loginActivity)
我找到的最简单的解决方案是简单地在具有焦点的控件上调用 Unfocus()
方法(例如 Entry
或 Editor
)。如果您想直接在 XAML 中指定函数,可以将此调用嵌入到 UnfocusOnClicked 行为中。