如何在单击Edittext外部后在Xamarin Android中隐藏键盘

时间:2016-09-22 10:33:44

标签: c# android xamarin.android android-softkeyboard

我正在开发 Xamarin(Android)。现在我想在点击Edit Text之后隐藏键盘。

先谢谢。

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);
    }

1 个答案:

答案 0 :(得分:9)

使用此代码隐藏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;