이전 버전
개선 버전(키보드 깜빡이는 문제 개선)
public class MainActivity : MauiAppCompatActivity, IOnTouchListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Window.DecorView.ViewTreeObserver.GlobalFocusChange += FocusChanged;
}
private bool _keepFocus;
public override bool DispatchTouchEvent(MotionEvent e)
{
bool dispatch = base.DispatchTouchEvent(e);
if (e.Action == MotionEventActions.Down && CurrentFocus is not null)
{
if (!_keepFocus)
CurrentFocus.ClearFocus();
_keepFocus = false;
}
return dispatch;
}
private bool OnTouch(Android.Views.View v, MotionEvent e)
{
if (e.Action == MotionEventActions.Down && CurrentFocus == v)
_keepFocus = true;
return v.OnTouchEvent(e);
}
bool IOnTouchListener.OnTouch(Android.Views.View v, MotionEvent e)
{
return OnTouch(v, e);
}
private void FocusChanged(object sender, ViewTreeObserver.GlobalFocusChangeEventArgs e)
{
if (e.OldFocus is not null)
{
e.OldFocus.SetOnTouchListener(null);
}
if (e.NewFocus is not null)
{
e.NewFocus.SetOnTouchListener(this);
}
if (e.NewFocus is null && e.OldFocus is not null)
{
InputMethodManager imm = InputMethodManager.FromContext(this);
IBinder wt = e.OldFocus.WindowToken;
if (imm is null || wt is null)
return;
imm.HideSoftInputFromWindow(wt, HideSoftInputFlags.None);
}
}
}