binary input keypress event?

Paladium

Member
Licensed User
Longtime User
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
 

Paladium

Member
Licensed User
Longtime User
You should use the TextChanged event. If the new value is not "0" or "1" or "" you can set it be "".

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

Paladium

Member
Licensed User
Longtime User
...or that it only contains "0" and "1" characters.
If not revert to the "old value".

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

B4X founder
Staff member
Licensed User
Longtime User
by looping through the whole string, character by character?
Yes. It will be very fast.
B4X:
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
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

Paladium

Member
Licensed User
Longtime User
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
Top