我在外面点击

时间:2015-09-13 12:39:46

标签: c# android xamarin

我有EditText,当我点击它时我需要隐藏键盘 我怎么能这样做?

我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Content.PM;
using Android.Views.InputMethods;


namespace MurakamiKiev
{
    [Activity(Label = "Murakami", Icon = "@drawable/logo", Theme = "@android:style/Theme.Black.NoTitleBar", ScreenOrientation = ScreenOrientation.Portrait)]
    public class Cart2Activity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);



            SetContentView(Resource.Layout.Cart2);
            ImageButton nadislati = FindViewById<ImageButton>(Resource.Id.nadislatiButton);
            ImageButton logo = FindViewById<ImageButton>(Resource.Id.logoButton);
            ImageButton previous = FindViewById<ImageButton>(Resource.Id.previousButton);
            ImageButton home = FindViewById<ImageButton>(Resource.Id.homeButton);
            ImageButton menu = FindViewById<ImageButton>(Resource.Id.menuButton);

            //EditText misto = FindViewById<EditText>(Resource.Id.misto);


            nadislati.Click += delegate
            {
                var intent31 = new Intent(this, typeof(Cart3Activity));
                StartActivity(intent31);
            };
            previous.Click += delegate
            {
                var intent32 = new Intent(this, typeof(CartActivity));
                StartActivity(intent32);
            };
            //Otslezivaem click po knopke "Home" i perehodim na glavnuu
            home.Click += delegate
            {
                var intent33 = new Intent(this, typeof(MainActivity));
                StartActivity(intent33);
            };
            //Otslezivaem click po knopke "Logo" i perehodim na glavnuu
            logo.Click += delegate
            {
                var intent34 = new Intent(this, typeof(MainActivity));
                StartActivity(intent34);
            };
            menu.Click += delegate
            {
                var intent51 = new Intent(this, typeof(MenuTopActivity));
                StartActivity(intent51);

            };
        }
        public override bool OnTouchEvent(MotionEvent e)
        {
            
            EditText misto = FindViewById<EditText>(Resource.Id.misto);
            InputMethodManager inputManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);

            inputManager.HideSoftInputFromWindow(this.CurrentFocus.WindowToken, HideSoftInputFlags.NotAlways);

            return base.OnTouchEvent(e);
        }


    }
    }

我不明白,我的代码有什么问题?

我知道有很多这方面的帖子,但是有人没有帮助我。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

检查出来https://gist.github.com/elqsar/9278206我认为它会有用,但需要从java转换为c#

protected void setupParent(View view) {
    //Set up touch listener for non-text box views to hide keyboard.
    if(!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard();
                return false;
            }
        });
    }
    //If a layout container, iterate over children
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupParent(innerView);
        }
    }
}

  private void hideSoftKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager)   getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

答案 1 :(得分:0)

您的代码位于C#中。 @m一个java / android开发者,所以不能写代码,但我可以给你建议。 我认为您应该在onFocusChange()上使用Edittext(相应的C#函数)方法,而不是从那里隐藏焦点更改键盘。