负数输入字段的NativeScript键盘 [英] NativeScript Keyboard for Negative Number Input Field

查看:91
本文介绍了负数输入字段的NativeScript键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Nativescript to create an Android and iOS App. As it is a kind of a calculator there are a lot of input fields for numbers. The numbers are decimal and can be negative. So I need a Keyboard with numbers, decimal point and minus sign (dash).

因此,我使用键盘类型数字" :

在Android中打开的键盘很好. 但是iOS中的一个有很多不必要的字段,例如(". iOS键盘

The keyboard that opens up in Android is fine. But the one in iOS has a lot of unnecessary fields like "(". iOS KeyBoard

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

As i found out there is no keyboard in iOS with the fields that I want: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType

但是许多人将"DecimalPad"与包含减号的"inputAccessoryView"一起使用来解决此问题.在NativeScript中解决这个问题有好处吗?

But many people use the "DecimalPad" with a "inputAccessoryView" containing the minus sign to solve this problem. Is there a good was to solve this in NativeScript?

欢呼

推荐答案

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

You can use the native iOS API to create your custom keyboard. Here is an example that add a cancel button on your keypad and based on that concept you can fully customize your keyboard type.

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提供的源代码

The above example is based on the source code provided by @Luda here

有关如何将Objective-C转换"为NativeScript的更多信息,您可以找到

More information on how to "translate" Objective-C to NativeScript you can find here

这篇关于负数输入字段的NativeScript键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆