-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #863 from wechat-miniprogram/doc/fix-20240912
fix: inputfield文档修复与示例补充
- Loading branch information
Showing
4 changed files
with
889 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
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.
Oops, something went wrong.
Oops, something went wrong.