Entering Negative Numbers

devlei

Active Member
Licensed User
Longtime User
I would like the user to be able to enter positive or negative numbers in an EditText.
When I set the Input Type to 'Numbers' it will not accept the negative (-) sign before a number.
When I set the Input Type to 'DecimalNumbers' it will accept the negative, but I don't want the user to be able to enter decimals, only whole numbers.
When I set the Input Type to 'Text' it will accept the negative, but then the text keyboard pops up, and the user could enter text which would need to be checked with IsNumber - therefore not ideal.

I know I could use the Number Dialog, but it seems so clumsy to enter a simple negative number like -5.

Can someone help, cause I'm sure I am overlooking something basic?
 

Kevin

Well-Known Member
Licensed User
Longtime User
I'm having a similar but slightly different problem.

http://www.b4x.com/forum/basic4android-updates-questions/11404-numeric-keyboard-edittext.html#post63674

I think yours may have a simpler solution though. What I would do in your case is use DecimalNumbers but in the TextChanged event I would see what the last character entered was and check for anything you don't want allowed (such as a decimal) and if there, immediately take it back out through code.

I'm doing this now for my situation, but my only complaint is that I cannot use a numeric keyboard at all, so even though my users are entering an IP address (numbers and multiple decimals), they are greeted with the ALPHABETIC keyboard because I have found no other way to make it work. I know this should be possible because other apps show a numeric keyboard and allow multiple decimals in the EditText. I think it may be a current limitation in B4A but I am not sure. :confused:
 
Upvote 0

devlei

Active Member
Licensed User
Longtime User
Thanks for the response, Kevin.

I tried setting the Input Type to Phone Numbers. This results a different keyboard, but accepts negative numbers and also multiple decimals like an IP address. The period, however, is not on the popup keyboard, but on the desktop keyboard or the device keyboard.
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
The problem with the PHONE keyboard is that users can also enter an "N" "," ";" "(" ")" and a few other characters. THis can break your program if you try to run math on the number returned.

Here is what I did: I used the phone mode then used text.Replace() to filter out each of the unwanted characters from the final string that whas returned.

example:
Text = Text.Replace("N","")

It takes some extra code, but but only keeps the numbers and the "-"
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
What I don't like about using the phone keyboard is that the user then has to switch to a different keyboard to enter the decimals. A competing app to mine shows the same keyboard that appears with DecimalNumbers, but his allows multiple decimals. Although mine currently pops up the alpha keyboard, the user needs to switch to their own numeric keypad. I could use the phone pad, but the user would still then need to switch to another pad to enter decimals. I could convert asterisks (*) to decimals, but that would be kind of hokey to me. :D

As far as doing the Replace thing, that works too, but I find it more elegant to filter it on-the-fly and not let the user enter bad characters at all by removing them as they type. My app's current "live" version doesn't do filtering yet (this is a recent change).... My initial thought was that it should be obvious how to enter an IP address (and the "hint" gives an example) but I've had a few crash reports from people putting strange stuff in there for an IP address. Kind of amazed me. :D
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
Are you using the Dialog library? If so, how are you filtering on the fly?

ps. My phone keyboard has a "." Maybe others don't
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Are you using the Dialog library? If so, how are you filtering on the fly?

ps. My phone keyboard has a "." Maybe others don't

I guess I should have clarified, but I do the filtering on an EditText. I don't think it's possible with the dialogs, in which case I think that using Replace is about the only solution.

The phone keypad on my EVO doesn't have a period. My Swype keyboard has what appears to be a calculator-type mode, which is what appears when I set the EditText to DecimalNumbers, but again, no multiple periods/decimal points in an EditText. Must be a limitation in B4A, because not only the other app does it, but I just tried it on the Google search box on my phone and any number of decimals can be entered. I'm guessing perhaps Erel created some keyboard options for different scenarios (as options for an EditText) but didn't take into account the possibility of wanting to enter numbers and multiple decimal points. Hopefully in a very near future version of B4A? ;)
 
Upvote 0

ondesic

Active Member
Licensed User
Longtime User
That worked perfect!

You can even change the dialog textbox in a InputDialog to allow negative numbers by using this.
 
Upvote 0
Top