P Paladium Member Licensed User Longtime User Feb 16, 2011 #1 Hi, I read a lot in this forum, but found nothing that really satisfies me. So I need your help. I have an activity with two edit text fields. In the first the user should input binary (only 0 and 1!!!) and in the other one is the value of the first converted into decimal. How do I exactly accomplish an edit box where the user can only input 0 and 1. Ok, backspace and delete, too. Is there anybody who can help me with this? Thanks. Markus
Hi, I read a lot in this forum, but found nothing that really satisfies me. So I need your help. I have an activity with two edit text fields. In the first the user should input binary (only 0 and 1!!!) and in the other one is the value of the first converted into decimal. How do I exactly accomplish an edit box where the user can only input 0 and 1. Ok, backspace and delete, too. Is there anybody who can help me with this? Thanks. Markus
Erel B4X founder Staff member Licensed User Longtime User Feb 16, 2011 #2 You should use the TextChanged event. If the new value is not "0" or "1" or "" you can set it be "". Upvote 0
P Paladium Member Licensed User Longtime User Feb 16, 2011 #3 Erel said: You should use the TextChanged event. If the new value is not "0" or "1" or "" you can set it be "". Click to expand... Yes, but's not best practice I think. When you got a lot of 0's and 1's and then input one 5 in between them I have to use the replace function for all other types of characters?!? How would you do it? Edit Text: 000010100100101 and then insert: a 5 ->0000101050100101 Replace the 5 with ""? How for all other characters? Even "a","b","/",... whatever Markus Upvote 0
Erel said: You should use the TextChanged event. If the new value is not "0" or "1" or "" you can set it be "". Click to expand... Yes, but's not best practice I think. When you got a lot of 0's and 1's and then input one 5 in between them I have to use the replace function for all other types of characters?!? How would you do it? Edit Text: 000010100100101 and then insert: a 5 ->0000101050100101 Replace the 5 with ""? How for all other characters? Even "a","b","/",... whatever Markus
Erel B4X founder Staff member Licensed User Longtime User Feb 16, 2011 #4 I didn't understand your problem correctly. First set EditText.InputType to INPUT_TYPE_NUMBERS. Now in TextChanged check if the "new value" is an empty string or that it only contains "0" and "1" characters. If not revert to the "old value". Upvote 0
I didn't understand your problem correctly. First set EditText.InputType to INPUT_TYPE_NUMBERS. Now in TextChanged check if the "new value" is an empty string or that it only contains "0" and "1" characters. If not revert to the "old value".
P Paladium Member Licensed User Longtime User Feb 16, 2011 #5 Erel said: ...or that it only contains "0" and "1" characters. If not revert to the "old value". Click to expand... How would you do this? by looping through the whole string, character by character? B4A is very good, but I think an keypress event for views would be great. :sign0013: Markus Upvote 0
Erel said: ...or that it only contains "0" and "1" characters. If not revert to the "old value". Click to expand... How would you do this? by looping through the whole string, character by character? B4A is very good, but I think an keypress event for views would be great. :sign0013: Markus
Erel B4X founder Staff member Licensed User Longtime User Feb 16, 2011 #6 by looping through the whole string, character by character? Click to expand... Yes. It will be very fast. Code: Copy For i = 0 To New.Length - 1 If New.CharAt(i) <> "0" AND New.CharAt(i) <> "1" Then EditText1.Text = Old Return End If Next but I think an keypress event for views would be great Click to expand... Actually a solution based on TextChanged event will be easier to implement. Android handling of the virtual keyboard is different than Windows Mobile. TextChanged is the right approach when working with EditText views. Upvote 0
by looping through the whole string, character by character? Click to expand... Yes. It will be very fast. Code: Copy For i = 0 To New.Length - 1 If New.CharAt(i) <> "0" AND New.CharAt(i) <> "1" Then EditText1.Text = Old Return End If Next but I think an keypress event for views would be great Click to expand... Actually a solution based on TextChanged event will be easier to implement. Android handling of the virtual keyboard is different than Windows Mobile. TextChanged is the right approach when working with EditText views.
P Paladium Member Licensed User Longtime User Feb 16, 2011 #7 Thanks for the help. I did it already the same way you showed me. But after using the "old"-value, the cursor jumps in front of the first character. Can I change the cursor position? Keypress or TextChanged event. Who cares, if you can control the user input easily. Markus Upvote 0
Thanks for the help. I did it already the same way you showed me. But after using the "old"-value, the cursor jumps in front of the first character. Can I change the cursor position? Keypress or TextChanged event. Who cares, if you can control the user input easily. Markus
P Paladium Member Licensed User Longtime User Feb 16, 2011 #8 Paladium said: But after using the "old"-value, the cursor jumps in front of the first character. Can I change the cursor position? Click to expand... Found it: ed1.SelectionStart = ed1.Text.Length Markus Upvote 0
Paladium said: But after using the "old"-value, the cursor jumps in front of the first character. Can I change the cursor position? Click to expand... Found it: ed1.SelectionStart = ed1.Text.Length Markus