Can't get preferred keybpard type to display

Discussion in 'Jiggy Runtime Development' started by crimsondynamo329, Jul 27, 2008.

  1. crimsondynamo329

    crimsondynamo329 Member

    Joined:
    Jul 8, 2008
    Messages:
    493
    Likes Received:
    0
    Device:
    iPod touch
    I can't get the right type of keyboard to display. I want the numeric keyboard, but the regular one comes up. Here's the code:
    Code:
    var keyboard = new UIKeyboard;
    keyboard.position = [0,  window.bounds[3] - keyboard.bounds[3] ]
    keyboard.setDefaultReturnKeyType(4);
    keyboard.preferredKeyboardType = 1;
    keyboard.showPreferredLayout(true);
    mainView.addSubview(keyboard);
    keyboard.activate();
    What's wrong?

    Also, is there any way to make it so only numbers can be entered into a text field?

    Thanks
  2. PAEz

    PAEz New Member

    Joined:
    Jan 29, 2008
    Messages:
    201
    Likes Received:
    10
    saw this problem on jiggy devel group before and the answer was to use a with......

    Code:
    Plugins.load( "UIKit" );
    
    var window = new
    UIWindow( UIHardware.fullScreenApplicationContentRect );
    window.setHidden( false );
    window.orderFront();
    window.makeKey();
    window.backgroundColor = [ 1 , 1 , 1 , 1 ];
    
    var mainView = new UIView();
    window.setContentView( mainView );
    
    // Create an alert sheet
    var as = new UIAlertSheet( "" , [ "OK" , "Cancel"] , 1 );
    
    // Change the style
    as.setAlertSheetStyle( 4 );
    
    // Add a text fields
    var userTextField = as.addTextFieldWithValue( "" , "Enter a Phone Number" );
    
    // Change the keyboard type (2 = dialpad)
    with( userTextField.textTraits ) {
      preferredKeyboardType = 2;
    
    }
    
    // Handle alert sheet button click
    as.onButtonClicked = function( button )
    {
      if ( button == 1 )
      {
        log( "YOU PRESSED OK" );
    
        // We can get the text fields directly from the alert sheet
    
        log( "USER ID :" + as.textFieldAtIndex(0).text );
      }
      else
        log( "YOU PRESSED CANCEL" );
    
      // Get rid of the alert sheet
    
      as.dismiss( true );
    
    }
    
    mainView.onTap = function( )
    {
      as.popupAlertAnimated( true );
    
    }
    hope it helps.

    not sure on how to get it to be only numbers tho.

    edit:
    Well I cant get it to work with Alerts but heres a large example to show how to do it with a text input field....
    Code:
    Plugins.load( "UIKit" );
    
    var window = new
    UIWindow( UIHardware.fullScreenApplicationContentRect );
    window.setHidden( false );
    window.orderFront();
    window.makeKey();
    // Set the background to white, with a .75 alpha
    window.backgroundColor = [ 1 , 1 , 1, 0.75 ];
    
    // The regular width is 320 (window.bounds[2])
    var mainView = new UIScroller(0, 0, window.bounds[2],
    window.bounds[3]);
    mainView.setScrollingEnabled(false);
    
    // We'll make the size of the main window to be twice as tall as aregular window
    mainView.contentSize = [window.bounds[2], window.bounds[3] * 2];
    
    // Create our keyboard globally, so we can use it throughout theapplication
    var keyboard = new UIKeyboard();
    
    // Create a text field, with a white background, and a border.
    var text1 = createTextField([0, 48, 150, 50], "high");
    
    // Create a 2nd field, 100pix wide, 30 pixels tall
    var text2 = createTextField([50, 150, 100, 30], "mid");
    
    var text3 = createTextField([50, 400, 100, 30], "low");
    
    // add this field.
    mainView.addSubview(text1);
    mainView.addSubview(text2);
    mainView.addSubview(text3);
    
    window.setContentView( mainView );
    
    //
    // Just a convenience method to make textfields easier to create
    //
    function createTextField(coords, text)
    {
      var text1 = new UITextField(coords);
    
      text1.text = text;
      text1.setBorderStyle(1);
      text1.backgroundColor = [1, 1, 1, 1];
      text1.textColor = [.75, .75, 0, 1];
    
      // When focus comes to the field, we're showing the keyboard
      // at co-ordinates 0, 244 (meaning 0 pixels from left, and 244pixels from top)
      // This should line up the bottom of the keyboard with the bottom ofthe screen.
      text1.onBecomeFirstResponder = function()
      {
        // get the top left coordinates of this text field
        var topLeft = [this.frame[0], this.frame[1]];
        var keyboardTopLeft = [0, this.frame[1] + this.frame[3]];
        showKeyboard( topLeft, keyboardTopLeft);
      }
      text1.onResignFirstResponder = function()
    {mainView.scrollPointVisibleAtTopLeft( [0, 0], true );};
      text1.onShouldInsertText = respondToTextInput;
    
      return text1;
    
    }
    
    //
    // Alex Yianni
    // 08-Jan-2008
    //
    function showKeyboard(topCoords, keyboardCoords)
    {
       keyboard.preferredKeyboardType = 1;
       keyboard.setDefaultReturnKeyType( 3 );
       keyboard.showPreferredLayout();
       mainView.addSubview( keyboard );
       keyboard.activate();
    
       // What we're doing here, is some trickery to make the keyboardappear at the bottom of the screen,
       // as well as change the content size to accomodate the startingtopCoords Y point.
       mainView.contentSize = [ mainView.bounds[ 2 ] ,
    mainView.bounds[ 3 ] + keyboardCoords[1] ];
       mainView.scrollPointVisibleAtTopLeft( topCoords, true );
       keyboard.position = keyboardCoords;
    
    }
    
    //
    // As the function name suggests, this hides the keyboard
    //
    function hideKeyboard(coords)
    {
       keyboard.deactivate();
       keyboard.removeFromSuperview();
    
       // After removing the keyboard, we want to set the focus point tobe at the very top left (0, 0).
       // The reason for this, is that we're taking scrolling off, andI've found that you can lock yourself on
       // an empty page.
       mainView.scrollPointVisibleAtTopLeft( coords , true );
       mainView.setScrollingEnabled(false);
    
    }
    
    function respondToTextInput(text , isMarked)
    {
        if ( text != "\n" ){
      if (text==" ") return false;    
     if (isFinite(text)) return true; else return false;
    }
        if ( text.length == 0 )
                return false;
    
        // Note.. 'this' is referring to a text field, as this function islikely to be assigned.
        this.resignFirstResponder();
        hideKeyboard([0, 0]);
        return false;
    
    } 
    ....heres the bit that makes it only allow numbers.....
    Code:
    function respondToTextInput(text , isMarked)
    {
        if ( text != "\n" ){
     if (text==" ") return false;     
     if (isFinite(text)) return true; else return false;
    }
        if ( text.length == 0 )
                return false;
    
        // Note.. 'this' is referring to a text field, as this function islikely to be assigned.
        this.resignFirstResponder();
        hideKeyboard([0, 0]);
        return false;
    
    }
    ...well this bit to be exact....
    Code:
        if ( text != "\n" ){
     if (text==" ") return false;  
     if (isFinite(text)) return true; else return false;
    }
  3. crimsondynamo329

    crimsondynamo329 Member

    Joined:
    Jul 8, 2008
    Messages:
    493
    Likes Received:
    0
    Device:
    iPod touch
    Thank you very much! This should help, once I figure out how to put it into my app. I'm still not clear though, on how to change the keyboard type, if anyone can help.
  4. PAEz

    PAEz New Member

    Joined:
    Jan 29, 2008
    Messages:
    201
    Likes Received:
    10
    If your creating/managing the keyboard yourself then its...
    keyboard.preferredKeyboardType = 1;

    if you want to change the type of keyboard associated with a text field then its....
    with( userTextField.textTraits ) {
    preferredKeyboardType = 2;
    }

    the different numbers that can be used for preferredKeyboardType are listed here......
    http://jiggyapp.com/doc/files/UIKit/UIWindow-jsi.html#UIKeyboard.preferredKeyboardType
    ....you want, preferredKeyboardType = 2
  5. crimsondynamo329

    crimsondynamo329 Member

    Joined:
    Jul 8, 2008
    Messages:
    493
    Likes Received:
    0
    Device:
    iPod touch
    I'm lost!

    But if you look at my code
    Code:
    var keyboard = new UIKeyboard;
    keyboard.position = [0,  window.bounds[3] - keyboard.bounds[3] ]
    keyboard.setDefaultReturnKeyType(4);
    keyboard.preferredKeyboardType = 2;  // <---HERE------------------------------
    keyboard.showPreferredLayout(true);
    mainView.addSubview(keyboard);
    keyboard.activate();
    What is wrong? I have preferredKeyboardType 2, and I have showPreferredLayout. Am I missing something? Or is the order wrong? Because the regular keyboard keeps showing up. I don't ever need to hide it or make it pop up when entering text, it should always be there. I am completely lost on this!

    Please Register or Log in to view images

  6. PAEz

    PAEz New Member

    Joined:
    Jan 29, 2008
    Messages:
    201
    Likes Received:
    10
    If ya wanna pm me your code ill have a look at it, helps to see it with the rest of the code.....and worse come to worse Ill just adapt your code for that other keybaord code.

Share This Page