如何隐藏键盘在Xamarin Android后点击编辑文本

本文关键字:Android 文本 编辑 Xamarin 何隐藏 隐藏 键盘 | 更新日期: 2023-09-27 18:08:29

我正在研究Xamarin(Android) .现在我想在点击Edit Text后隐藏键盘。

Thanks in Advance.

public class MainActivity : Activity
{

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        RequestWindowFeature(WindowFeatures.NoTitle);

        SetContentView(Resource.Layout.Main);
        EditText Etusername= FindViewById<EditText>(Resource.Id.EtUname);
        Etusername.SetHintTextColor(Color.Gray);
        InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
        imm.HideSoftInputFromWindow(Etusername.WindowToken, 0);
    }

如何隐藏键盘在Xamarin Android后点击编辑文本

使用此代码隐藏Keyboard

public override bool OnTouchEvent(MotionEvent e)
    {
         InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
         imm.HideSoftInputFromWindow(Etusername.WindowToken, 0);
         return base.OnTouchEvent(e);
    }

,并确保你必须添加这个库:

using Android.Views.InputMethods;