Skip to content

InputField (JavaScript)

InputField

Scripting Name:Insight.InputField

输入框。

支持输入中英文、数字、符号、表情(emoji)。

  • _在 Web _SDK__中无效
  • 洞见__SDK_ v2.0 及以上_

Properties


interactable

type :boolean

输入框是否可交互。

text

type :string

输入框中的文字。

javascript
this.inputField = this.game_object.getComponent("InputField");Insight.Debug.Log("text: " .. this.inputField.text);this.inputField.text = "Hello ARScript";

count

type :number

(只读)输入框中文字的个数。

Methods


toString

javascript
public string toString();

将 InputField 内容以一定格式转为 string 类型。

setTextWithoutNotify

javascript
public void setTextWithoutNotify(string text);

设置输入框中的文字,但是不触发输入框内容改变的事件。

Parameters

  • text : string 设置的文字内容

Static Methods


OnEndEdit

javascript
public static void OnEndEdit( Insight.InputField inputField, module_instance, func );

注册输入完成的监听事件。在键盘收起时调用。

Parameters

  • button : Insight.InputField 要注册点击事件的按钮
  • module_instance 脚本本身实例
  • func 回调的 js 方法
    • 方法传入参数:string - 输入框中现有的文本内容

OnValueChanged

javascript
public static void OnValueChanged( Insight.InputField inputField, module_instance, func );

注册文本变更的监听事件。每输入或删除字符都会调用。

Parameters

  • inputField : Insight.InputField 要注册点击事件的按钮
  • module_instance 脚本本身实例
  • func 回调的 js 方法
    • 方法传入参数:string - 输入框中现有的文本内容

OnValidateInput

javascript
public static void OnValidateInput( Insight.InputField inputField, module_instance, func );

注册文本检测的监听事件。用于自定义的文本检测(该方法会屏蔽在 Unity 中设置的 ContentType 选项)。

Parameters

  • inputField : Insight.InputField 要注册点击事件的按钮
  • module_instance 脚本本身实例
  • func 回调的 js 方法
    • 方法传入参数 1:string - 输入框中以后的文本内容
    • 方法传入参数 2:number - 最新输入的待检测字符在所有文本中的位置索引(从 0 开始)
    • 方法传入参数 3:string - 最新输入的待检测字符
    • 方法返回值:检测后的显示字符(若返回多个字符,只会取第一个)

示例如下:

javascript
var InputTest = function(game_object){   this.game_object = game_object;}
InputTest.prototype = Object.assign(Object.create(Object.prototype), {  Start: function(){    Insight.Debug.Log("inputField Start");    this.inputField = this.game_object.getComponent("InputField");    if(this.inputField == undefined)      Insight.Debug.Log("inputField error");    Insight.Debug.Log("text: " .. this.inputField.text);    Insight.InputField.OnEndEdit( this.inputField, this, this.onEndEdit);    Insight.InputField.OnValueChanged( this.inputField, this, this.onValueChanged);    Insight.InputField.OnValidateInput( this.inputField, this, this.onValidateInput);  }  // 输入完成的回调方法
  onEndEdit: function( content )    {    Insight.Debug.Log("onEndEdit: " .. content);  }  // 输入变更的回调方法
  onValueChanged: function( content )    {    Insight.Debug.Log("onValueChanged: " .. content .. " count: " .. this.inputField.count);    }    // 输入文本检测的回调方法
  // 检测输入的字符是否是数字,如果是数字就显示成"*"
  onValidateInput: function( text, index, char)    {      Insight.Debug.Log("onValidateInput: " .. text .. " " .. index .. " " .. char);    if tonumber(char) then
        return '*';    return char;  }}
InputTest

Inherited Properties(继承自 Component)


enabled

type : boolean

该组件是否是可用的。

gameObject

type :Insight.GameObject

(只读)该组件所依附的 GameObject。

isActiveAndEnabled

type : boolean

(只读)该组件是否可用,且其依附的 GameObject 是否是活跃状态。

name

type : string

(只读)所依附的 GameObject 的名称。

tag

type : string

(只读)所依附的 GameObject 的 Tag。

⚠️ 暂时未启用

transform

type :Insight.Transform

(只读)所依附的 GameObject 的 transform。

Messages


OnPointerDown

手指按下事件

OnPointerUp

手指抬起事件

javascript
var InputTest = function(game_object){   this.game_object = game_object;}
InputTest.prototype = Object.assign(Object.create(Object.prototype), {  Start: function(){    //code
  }  // 输入框检测到手指按下
  OnPointerDown: function()    {    Insight.Debug.Log( "JS InputFieldClick Update: GetPointerDown\n" );  }    // 输入框检测到手指抬起
  OnPointerUp: function()    {    Insight.Debug.Log( "JS InputFieldClick Update: GetPointerUp\n" );  }}
InputTest