在ios钛上隐藏键盘

时间:2016-06-11 02:33:05

标签: titanium appcelerator titanium-mobile appcelerator-mobile appcelerator-titanium

在我的钛金属应用程序上,我有一个包含许多字段的表单(文本字段等...),当我专注于文本字段时,它会显示ios键盘,当我点击窗口某处时我想隐藏它:

<Alloy>
  <Window id="home" >
    <View id="form">
      <Require type="view" id="myViewForm" src="form/etape_1" />
    </View>
  </Window>
</Alloy>

在myViewForm中:

<Alloy>
    <View>
      <TextField id="name" hintText="name"/>
      <TextField id="telephone" hintText="Téléphone"/>
    </View>
</Alloy>

注意:如您所见,我有一个带有ID&#34的文本字段;电话&#34;这只会显示数字。

在我的控制器主文件中:

/*-----------------------------------------
|   |   EVENT LISTENER CLICK ON WINDOW
-------------------------------------------*/
$.home.addEventListener("click", hideSoftKeyboard);
/*-----------------------------------------
|   |   HIDE KEYBOARD 
-------------------------------------------*/
function hideSoftKeyboard(e){
    if(Ti.Platform.osname === 'android'){
         Ti.UI.Android.hideSoftKeyboard();
    } else {
        $.home.textField.blur();
    }
}

在Android上运行良好,但在Ios上我有以下错误:

[ERROR] :  Script Error {
[ERROR] :      column = 103;
[ERROR] :      line = 12;
[ERROR] :      message = "undefined is not an object (evaluating '$.home.textField.blur')";
[ERROR] :      stack = hideSoftKeyboard;
[ERROR] :  }

有人可以帮我吗?谢谢。

2 个答案:

答案 0 :(得分:3)

以下是您的代码:

<强> index.js

$.home.addEventListener("click", hideSoftKeyboard);

$.home.open();

function hideSoftKeyboard(e){
    if(OS_ANDROID){
         Ti.UI.Android.hideSoftKeyboard();
    } else {
        $.myViewForm.name.blur();
        $.myViewForm.telephone.blur();
    }
}
  • 但是还要记住,如果你没有设置 在文本字段上bubbleParent="false",只要您点击文字字段,就会启动您在窗口上的点击事件。
  • 因此请注意在整个窗口中使用click事件。
  • 如果您在窗口上使用点击事件的目的只是使用手机布局键盘隐藏iOS上的键盘,那么我建议您查看此属性TextField keyboardToolbar,这将很好地满足您的目的。

以下是隐藏电话键盘的完整跨平台代码。

<强> index.js

$.home.open();

<强> myViewForm.xml

<Alloy>
    <View layout='vertical'>
        <TextField id="MOBILE_FIELD" class="phone fields" platform="android" />

        <TextField id="MOBILE_FIELD" class="phone fields" platform="ios">
            <KeyboardToolbar>
                <Toolbar>
                    <Items>
                        <FlexSpace />
                        <Button title="DONE" onClick="hideKeyboard" />      
                    </Items>
                </Toolbar>
            </KeyboardToolbar>
        </TextField>
    </View>
</Alloy>

<强> myViewForm.tss

// to accept only phone numbers, with + sign also..
".phone[platform=ios]" : {
    keyboardType : Ti.UI.KEYBOARD_TYPE_NUMBER_PAD,
}

".phone[platform=android]" : {
    inputType : [Ti.UI.INPUT_TYPE_CLASS_NUMBER]
}


".fields" : {
    top : 30
    width : '80%',
    height : 50,
    borderColor : 'black',
    borderWidth : 2
}

<强> myViewForm.js

function hideKeyboard() {
    $.MOBILE_FIELD.blur();
}

答案 1 :(得分:-2)

尝试

$.myViewForm('name').blur();
$.myViewForm('telephone').blur();

取代$.home.textField.blur();

$.myViewForm('name')将获取ID为name的视图,该视图将成为您的文本字段,然后您可以在其上调用blur()方法。

希望这会对你有所帮助。

相关问题