Пишу приложение для Андроида и возник вопрос, как скрыть клавиатуру при нажатии вне EditText
?
Насколько я читал можно это делать через OnTouchListener
? Тогда вопрос в том, как его повесить на LinearLayout
?
Пишу в Xamarin на C#. Собственно, мой код:
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);
}
}
}
<>
для оформления кода. Она только для сниппетов на JS-HTML-CSS, которые могут прямо в браузере выполняться. Для обычного кода используйте кнопку{}
, или просто отступ в 4 пробела. Пока что я отредактировал ответ, поправив форматирование. – Nick Volynkin♦ 13 сен '15 в 14:07