如何在Xamarin Android中点击EditText外部后隐藏键盘

5
我正在使用Xamarin(Android)进行开发。现在我想要在点击EditText以外的区域后隐藏键盘。
提前感谢。
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个回答

10

使用此代码来隐藏 键盘

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;

感谢您的回复。我刚刚尝试了上面的代码,但没有任何改变。键盘仍然出现。 - raji
2
@raji 很高兴能帮助你。 - Harshad Pansuriya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接