iPhone UIWebview: How to force a numeric keyboard? Is it possible?

后端 未结 10 1780
清酒与你
清酒与你 2020-12-02 09:34

I\'m experimenting with PhoneGap to develop some iPhone apps. PhoneGap basically wraps a UIWebView - it works well. The problem is the my app has several input fields that

10条回答
  •  清歌不尽
    2020-12-02 10:35

    iOs has a numeric keyboard UIKeyboardTypeNumbersAndPunctuation, but this can't be called from HTML (https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html)

    since on the "tel" keyboard the punctuation is missing you have to create this on your own. Since ios8 custom keyboards are available. Or if you just need the punctuation you can add a button (How to add a 'Done' button to numpad keyboard in iOS) to the numeric keyboard which pops up when you specify your input field with type="number" pattern="[0-9]*" .

    To do so: the objective-c code.

    -(void)keyboardWillHide:(NSNotification *)note
    {
        [self.donekeyBoardBtn removeFromSuperview];
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat: @"document.activeElement.blur()"]];
    }
    
    - (void)keyboardWillShow:(NSNotification *)note
    {
        [UIView setAnimationsEnabled:NO];
        // create custom button
        //UIKeyboardTypeNumberPadin
        //type="number" pattern="[0-9]*"
        UIButton *extryB= [UIButton buttonWithType:UIButtonTypeRoundedRect];
        extryB.frame = CGRectMake(0, self.view.frame.size.height+150, 100, 50);
        extryB.backgroundColor = [UIColor colorWithRed:204.0f/255.0f
                                                 green:210.0f/255.0f
                                                  blue:217.0f/255.0f
                                                 alpha:1.0f];
        extryB.adjustsImageWhenHighlighted = NO;
        extryB.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [extryB setTitle:@"," forState:UIControlStateNormal];
        [extryB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [extryB addTarget:self action:@selector(extryBpressed) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.donekeyBoardBtn = extryB;
    
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++)
    {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
    
        if([[keyboard description] hasPrefix:@"

    Then you should add the a method which can be accessed application wide in your JS code.

    simulateKeyPress: function(k) {
            var press = jQuery.Event("keypress");
    
            press.which = k;
            // place the id of your most outer html tag here
            $("#body").trigger(press);
                // just needed because my active element has a child element where the value should be placed in
                    var id = document.activeElement.id.indexOf("-");
                    if(id != -1){
                        var currentTf = sap.ui.getCore().byId(document.activeElement.id.split("-")[0]);
                        //append the value and set it (my textfield has a method setValue())
                        var currentTfValue = currentTf.getValue();
                        currentTf.setValue(currentTfValue + k);
                    }
                },
    

提交回复
热议问题