tags:

views:

84

answers:

2

I'm trying to take the user input, which may or may not have a comma in it, and put a comma in the correct places upon the user deselecting the field (or at all, if that's not possible).

I would also like to know how I can subtract the commas to make the number just an integer.

Thanks in advance for your help!

A: 

I don't know about Android specifically, but if you are programming in Swing then you probably want to look at javax.swing.InputVerifier. It triggers a verification check on a Swing component when it is unfocussed, which I believe includes the ability to modify the contents. An extra advantage is being able to check that the text field really does contain a number.

DJClayworth
Thank you, DJ, but I am using Android specifically, and I don't think it allows for me to use Swing components. :(I need the equivalent of your solution, only for Android! It would be perfect.
Allen Gingrich
A: 

I don't know for certain but I can speculate. Add a focus listener and implement all the logic associated with modifying the content of the TextView in the listener.

As far as commas go, I assume you store the value as a String. So, you can use NumberFormat, parse the String and then call intValue method

NumberFormat format = NumberFormat.getNumberInstance();
String numAsStr = "1,000,000";
int num = format.parse(numAsStr).intValue();

You can use NumberFormat.format to format the input:

format.format(1000000)//will yield a string "1,000,000"
Andrey
originally I indicated you should try onWindowFocusChanged, but you probably want to use setOnFocusChangeListener (I changed the link to reflect the change)
Andrey