在MvxFragment中关闭/隐藏Android软键盘
我用xamarin+mvvmcross创建android应用程序。我的MvxFragment中有一个MvxAutoCompleteTextView。在MvxAutoCompleteTextView中写入并单击其他控件后,我想隐藏虚拟键盘。我使用这个代码在MvxFragment中关闭/隐藏Android软键盘,android,xamarin,mvvmcross,android-softkeyboard,Android,Xamarin,Mvvmcross,Android Softkeyboard,我用xamarin+mvvmcross创建android应用程序。我的MvxFragment中有一个MvxAutoCompleteTextView。在MvxAutoCompleteTextView中写入并单击其他控件后,我想隐藏虚拟键盘。我使用这个代码 public class MyFragment : MvxFragment { public override View OnCreateView(LayoutInflater inflater, ViewGroup container
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;
}
}
公共类MyFragment:MvxFragment
{
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
base.OnCreateView(充气机、容器、保存状态);
var view=this.BindingInflate(Resource.Layout.frMy,null);
var autoComplete=view.findviewbyd(Resource.Id.acMy);
InputMethodManager inputManager=(InputMethodManager)inflater.Context.GetSystemService(Context.InputMethodService);
inputManager.HideSoftInputFromWindow(autoComplete.WindowToken,HideSoftInputFlags.None);
返回视图;
}
}
但这不起作用。如何隐藏键盘?试试我的功能:
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);
}
对不起,我在安卓工作室工作,请告诉我我是否帮助过你,以及我的编程能力 您可以隐藏软键盘,将焦点放在非“键盘启动器”控件的内容上,例如,自动完成控件的父容器
parentContainer = FindViewById<LinearLayout>(Resource.Id.parentContainer);
parentContainer.RequestFocus();
parentContainer=findviewbyd(Resource.Id.parentContainer);
parentContainer.RequestFocus();
假设您的父容器是一个线性布局,您应该允许它通过以下两个属性获得焦点:
<LinearLayout
android:id="@+id/parentContainer"
android:focusable="true"
android:focusableInTouchMode="true">
试试这个:
HideSoftInputFromWindow
中的值0
是常量Android.Views.InputMethods.HideSoftInputFlags.None
,因此可以使用等效语法:
我从这里得到了java的答案: 以下是C#版本(已测试并正在运行): 如果您单击非编辑文本的任何内容,这段代码将隐藏软键盘。 您只需将其粘贴到活动类中(例如,您的loginActivity)
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, 0);
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
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);
}