Hide keyboard on ios titanium

user3137766

On my titanium app i have a form with many fields (textfield etc...), when i focus on textfield it shows the ios keyboard and i want to hide it when i click somewhere on the window :

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

inside myViewForm :

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

Note : As you see i have a textfield with id "telephone" that will show only numbers.

on my controller home file :

/*-----------------------------------------
|   |   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();
    }
}

On android it works well, but on Ios i have following error :

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

Someone could help me please ? thank you.

Prashant Saini

Here is the code for you:

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();
    }
}
  • But also remember that if you do not set bubbleParent="false" on text-fields, then your click event on window will be fired up as soon as you click on text-field.
  • So be careful about using click event on whole window.
  • If your purpose of using click event on window is just to hide keyboard on iOS using phone layout keyboard, then I will suggest you to look this property TextField keyboardToolbar which will serve your purpose well.

Here is the complete cross-platform code to hide telephone keyboards.

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();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hide individual keyboard buttons in iOS

From Dev

Hide individual keyboard buttons in iOS

From Dev

Hide keyboard in iOS for any view?

From Dev

iOS: listener of the "hide" button in the keyboard

From Dev

How to Show and Hide UIPickerView like iOS Keyboard?

From Dev

iOS 7: How to hide the DONE button on the keyboard

From Dev

Hide shortcut keyboard bar for UIWebView in iOS 9

From Dev

Stripe layout ios keyboard hide by default.

From Dev

Is resignFirstResponder needed in iOS7 to hide keyboard?

From Dev

hide shortcuts bar when hide keyboard ios(Ipad)

From Dev

iOS 8 - How to hide suggestion list above keyboard?

From Dev

Hide cut/paste panel from keyboard in iOS 9

From Dev

how to hide keyboard when i touch screen in iOS

From Dev

Hide footer when keyboard appear in IOS App via IONIC

From Dev

Can't hide iOS Keyboard (xcode 5) in current project

From Dev

Quit application in titanium (iOS)

From Dev

Titanium ios latest modules

From Dev

How to detect the keyboard on the android window - Titanium Appcelerator

From Dev

How to hide keyboard in UITableViewController?

From Dev

Nativescript Android - hide keyboard

From Dev

flutter :Hide keyboard for a TextField

From Dev

Keyboard hide event in Android

From Dev

Hide keyboard autocompletetextview

From Dev

On Button Click Hide Keyboard

From Dev

Android hide keyboard

From Dev

Hide a keyboard return button

From Dev

Can't hide titlebar Titanium with Alloy

From Dev

Add image to Titanium iOS module

From Dev

Extending Titanium with native iOS modules