Skip to content

fix: inputfield文档修复与示例补充 #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading