【问题标题】:NativeScript Keyboard for Negative Number Input Field负数输入字段的 NativeScript 键盘
【发布时间】:2016-09-14 21:26:50
【问题描述】:

我正在使用 Nativescript 创建一个 Android 和 iOS 应用程序。由于它是一种计算器,因此有很多数字输入字段。 数字是十进制的,可以是负数。所以我需要一个带有数字、小数点和减号(破折号)的键盘。

因此我使用键盘类型“数字”

在 Android 中打开的键盘很好。 但是iOS中的那个有很多不必要的字段,例如“(”。iOS KeyBoard

我发现 iOS 中没有带有我想要的字段的键盘: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType

但很多人使用“DecimalPad”和包含减号的“inputAccessoryView”来解决这个问题。在 NativeScript 中解决这个问题有什么好办法吗?

干杯

【问题讨论】:

    标签: ios input keyboard numbers nativescript


    【解决方案1】:

    您可以使用原生 iOS API 创建自定义键盘。 这是一个在您的键盘上添加取消按钮的示例,基于该概念,您可以完全自定义您的键盘类型。

    page.xml

    <Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="onLoaded">
      <StackLayout>
        <TextField hint="" text="1425" id="tf-toolbar" />
      </StackLayout>
    </Page>
    

    page.js

    function onLoaded(args) {
        var page = args.object;
    
        // if app.ios ...
        var myTextField = page.getViewById("tf-toolbar");
    
        // for the sake of example no target and actions are set (null)
        var barButtonItemOne = UIBarButtonItem.alloc().initWithTitleStyleTargetAction("Cancel", UIBarButtonItemStyle.UIBarButtonItemStyleBordered, null, null);
        var items = NSArray.arrayWithObject(barButtonItemOne);
    
        var toolbar = UIToolbar.alloc().initWithFrame({ origin: { x: 0, y: 0 }, size: { width: 320, height: 150 } });
        toolbar.barStyle = UIBarStyle.UIBarStyleBlackTranslucent;
        toolbar.items = items;
    
        toolbar.sizeToFit();
    
        var iosText = myTextField.ios;
        iosText.inputAccessoryView = toolbar;
    
    }
    exports.onLoaded = onLoaded;
    

    以上示例基于@Luda here提供的源码

    有关如何将 Objective-C “翻译”为 NativeScript 的更多信息,您可以找到 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 2011-02-27
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多