UnityPlatformAndroidTextInput.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if (UNITY_ANDROID)
  2. #if HZMFUI
  3. using CommonUI.UI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace CommonUI_Unity3D_Android
  9. {
  10. public class UnityPlatformAndroidTextInput :MonoBehaviour
  11. {
  12. private UITextInput mInput = null;
  13. private int mMaxLength = 100;
  14. private string mText = "";
  15. protected TouchScreenKeyboard mKeyboard;
  16. public void SetInput(UITextInput input)
  17. {
  18. //if input = null then seems to close KeyBoard.
  19. if(input == null && mInput != null && !mInput.IsDispose)
  20. {
  21. mInput.FadeTextField(false);
  22. mInput.Text = mText;
  23. mInput.SetInputFinish(mText);
  24. if(mKeyboard != null)
  25. {
  26. mKeyboard.active = false;
  27. }
  28. }
  29. mInput = input;
  30. //open KeyBoard.
  31. if(mInput != null && !mInput.IsDispose)
  32. {
  33. mInput.FadeTextField(true);
  34. mMaxLength = input.MaxLength;
  35. mText = mInput.Text;
  36. mKeyboard = TouchScreenKeyboard.Open(mText, ConvertKeyBoardType(mInput.InputType), false);
  37. }
  38. }
  39. void Update()
  40. {
  41. if(mKeyboard != null)
  42. {
  43. //update Content.
  44. string text = mKeyboard.text;
  45. if(text == null)
  46. {
  47. text = "";
  48. }
  49. if(mText != text && mInput != null && !mInput.IsDispose)
  50. {
  51. mText = "";
  52. for(int i = 0; i < text.Length; ++i)
  53. {
  54. char ch = text[i];
  55. ch = mInput.DoValidator(mText, ch);
  56. if(ch != 0)
  57. mText += ch;
  58. }
  59. if(mMaxLength > 0 && mText.Length > mMaxLength)
  60. mText = mText.Substring(0, mMaxLength);
  61. if(mText != text)
  62. mKeyboard.text = mText;
  63. UpdateInputText();
  64. }
  65. //check input status.
  66. if(mKeyboard.done || !mKeyboard.active)
  67. {
  68. mKeyboard = null;
  69. this.SetInput(null);
  70. }
  71. }
  72. }
  73. private TouchScreenKeyboardType ConvertKeyBoardType(UITextInput.KeyBoardType type)
  74. {
  75. TouchScreenKeyboardType rlt = TouchScreenKeyboardType.Default;
  76. switch(type)
  77. {
  78. case UITextInput.KeyBoardType.NumberPad:
  79. rlt = TouchScreenKeyboardType.NumberPad;
  80. break;
  81. case UITextInput.KeyBoardType.PhonePad:
  82. rlt = TouchScreenKeyboardType.PhonePad;
  83. break;
  84. case UITextInput.KeyBoardType.EmailAddress:
  85. rlt = TouchScreenKeyboardType.EmailAddress;
  86. break;
  87. default:
  88. break;
  89. }
  90. return rlt;
  91. }
  92. private void UpdateInputText()
  93. {
  94. mInput.Text = mText;
  95. }
  96. }
  97. }
  98. #endif
  99. #endif