简体   繁体   English

如何在EditText中输入“负十进制数”?

[英]How to enter a “Negative Decimal Number” in EditText?

I am making a calculator and I want to make sure that user is able to enter a "Negative Decimal Number" I see the option in the xml file separately for example in order to put the Decimal Number, the EditText's inputType must be numberDecimal and for negative number the inputType must be "numberSigned" but I need both. 我正在制作一个计算器,我想确保用户能够输入“负十进制数”我单独看到xml文件中的选项,例如为了放入十进制数,EditText的inputType必须是numberDecimal,并且负数,inputType必须是“numberSigned”,但我需要两者。 If I choose inputType as "text" then user is able to enter anything which throws an exception if it's not a number. 如果我选择inputType作为“text”,那么如果不是数字,用户可以输入抛出异常的任何内容。 I am not sure how to fix that either. 我不知道如何解决这个问题。

You can use both with the "|" 您可以同时使用“|” operator. 运营商。 Just specify "numberSigned|numberDecimal". 只需指定“numberSigned | numberDecimal”即可。

Add this in the layout of the EditText : EditText的布局中添加:

android:digits="0123456789-."

You can specify more there, for example if you want the user to input sin() functions. 您可以在那里指定更多,例如,如果您希望用户输入sin()函数。

As @a11n says, you can use the pipe operator to specify both: 正如@ a11n所说,您可以使用管道运算符来指定:

<EditText
    ...
    android:inputType="numberSigned|numberDecimal"
    ... />

Valid numbers: positive and negative decimal and whole numbers. 有效数字:正数和负数十进制数和整数。

Some phones have the decimal and negative in the same button and makes it unable to do negatives. 有些手机在同一个按钮中有小数和负数,因此无法进行否定。 I figured out a way you could separate the buttons by simply adding: 我想通过简单地添加以下方法来分离按钮:

android:inputType="numberSigned|numberDecimal|textPersonName"
android:digits="0123456789.-"

That way you still have the number keyboard layout but the negative and decimal buttons will be separate and you cannot use any other digit that messes up the app. 这样你仍然有数字键盘布局,但负和十进制按钮将是独立的,你不能使用任何其他数字搞砸应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM