在输入最后一个OTP数字时关闭键盘



我正在研究一个有OTP屏幕的概念。要求是在输入6位OTP号码的最后一位时自动关闭键盘。

以下是我到目前为止所做的-

if (lastOTPEntry.Value != string.Empty)
{
lastOTPEntry.Unfocus();
}

然后我有一个EntryRenderer,它覆盖了这个方法-

protected override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e). 
{ 
if (Control != null). 
{    
if (e.Focus)
{
Control.RequestFocus();
}
else
{
Control.ClearFocus();
}
}
}

ControlFormsEditText但不知何故,键盘并没有消失。我在这里做错了什么?

我需要在旧项目中做一些类似的事情,最终使用了一个服务模式来实现。

安卓服务:

public class KeyboardService : IKeyboardService
{        
public void HideKeyboard()
{
var context = Application.Context;
var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null && context is Activity)
{
var activity = context as Activity;
var token = activity.CurrentFocus?.WindowToken;
inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
activity.Window.DecorView.ClearFocus();
}
}
}

iOS服务:

public class KeyboardService : IKeyboardService
{
public void HideKeyboard()
{
UIApplication.SharedApplication.KeyWindow.EndEditing(true);
}
}

接口:

public interface IKeyboardService
{
void HideKeyboard();
}

要使用,你需要向你的依赖服务注册它来解决它。在它被解决后,要使用它:

if (lastOTPEntry.Value != string.Empty)
{
keyboardService.HideKeyboard();
}

我已经有一段时间没有接触这个项目了,但逻辑应该仍然有效。

最新更新