Fet*_*mos 7 android android-softkeyboard mvvmcross xamarin
我用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;
}
}
Run Code Online (Sandbox Code Playgroud)
但这不行.如何隐藏键盘?
您可以隐藏软键盘,将焦点放在不是"键盘启动器"控件的位置,例如,自动完成控件的父容器.
parentContainer = FindViewById<LinearLayout>(Resource.Id.parentContainer);
parentContainer.RequestFocus();
Run Code Online (Sandbox Code Playgroud)
假设您的父容器是LinearLayout,您应该允许它使用这两个属性获得焦点:
<LinearLayout
android:id="@+id/parentContainer"
android:focusable="true"
android:focusableInTouchMode="true">
Run Code Online (Sandbox Code Playgroud)
我从这里得到了答案:https://stackoverflow.com/a/28939113/4664754
这是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);
}
Run Code Online (Sandbox Code Playgroud)
如果您单击任何不是EditText的内容,这段代码将隐藏软键盘.您只需将其粘贴到您的活动类中(例如您的loginActivity)
尝试这个:
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, 0);
Run Code Online (Sandbox Code Playgroud)
0中的值HideSoftInputFromWindow是 const Android.Views.InputMethods.HideSoftInputFlags.None,因此您可以使用等效语法:
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(autoComplete.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7842 次 |
| 最近记录: |