Skip to content

Commit

Permalink
Merge pull request #863 from wechat-miniprogram/doc/fix-20240912
Browse files Browse the repository at this point in the history
fix: inputfield文档修复与示例补充
  • Loading branch information
0oQiaoo0 authored Sep 12, 2024
2 parents 75a80ee + 1ff86f1 commit cf66a55
Show file tree
Hide file tree
Showing 4 changed files with 889 additions and 341 deletions.
88 changes: 88 additions & 0 deletions Demo/API_V2/Assets/API/InputField/WXInputFieldAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using UnityEngine;
using WeChatWASM;
using UnityEngine.UI;
using UnityEngine.EventSystems;

// 添加 InputField 组件的依赖
[RequireComponent(typeof(InputField))]
public class WXInputFieldAdapter : MonoBehaviour, IPointerClickHandler, IPointerExitHandler
{
private InputField _inputField;
private bool _isShowKeyboard = false;

private void Start()
{
_inputField = GetComponent<InputField>();
}

public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("OnPointerClick");
ShowKeyboard();
}

public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("OnPointerExit");
if (!_inputField.isFocused)
{
HideKeyboard();
}
}

private void OnInput(OnKeyboardInputListenerResult v)
{
Debug.Log("onInput");
Debug.Log(v.value);
if (_inputField.isFocused)
{
_inputField.text = v.value;
}
}

private void OnConfirm(OnKeyboardInputListenerResult v)
{
// 输入法confirm回调
Debug.Log("onConfirm");
Debug.Log(v.value);
HideKeyboard();
}

private void OnComplete(OnKeyboardInputListenerResult v)
{
// 输入法complete回调
Debug.Log("OnComplete");
Debug.Log(v.value);
HideKeyboard();
}

private void ShowKeyboard()
{
if (_isShowKeyboard) return;

WX.ShowKeyboard(new ShowKeyboardOption()
{
defaultValue = "xxx",
maxLength = 20,
confirmType = "go"
});

//绑定回调
WX.OnKeyboardConfirm(this.OnConfirm);
WX.OnKeyboardComplete(this.OnComplete);
WX.OnKeyboardInput(this.OnInput);
_isShowKeyboard = true;
}

private void HideKeyboard()
{
if (!_isShowKeyboard) return;

WX.HideKeyboard(new HideKeyboardOption());
//删除掉相关事件监听
WX.OffKeyboardInput(this.OnInput);
WX.OffKeyboardConfirm(this.OnConfirm);
WX.OffKeyboardComplete(this.OnComplete);
_isShowKeyboard = false;
}
}
11 changes: 11 additions & 0 deletions Demo/API_V2/Assets/API/InputField/WXInputFieldAdapter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf66a55

Please sign in to comment.