Automatically change backgound color of EditText

Mahares

Expert
Licensed User
Longtime User
I have a layout created via the designer that has 10 views of the type EditText (not initialized), that only accept numeric values. I would like to accomplish the following:
Whenever an Edittext box has focus= TRUE, I would like the background color of the box to change to cyan,. Whenever it loses focus (hasfocus=FALSE), I would like the background to change back to white. I would like it done programmatically and automatically rather than to repeat writing the code 10 times, once for each of the 10 EditText boxes like this:
EditText1_FocusChanged(Hasfocus As Boolean)
If hasfocus=true then
EditText1.Color=Colors.Cyan
Else
EditText1.Color=Colors.White
End Sub
I tried using the Sender feature, but was unsuccessful. Thank you very much for any tips or assistance.
 

Mahares

Expert
Licensed User
Longtime User
Here is how I was approacjing it, but it did not work.:

DIM DIFF2, RUN2, LINEP2 AS EditText
DIFF2.Initialize("MyEdit")
RUN2.Initialize("MyEdit")
LINEP2.Initialize("MyEdit")

Sub MyEdit_FocusChanged(Hasfocus As Boolean)
Dim Send As EditText
Send=Sender
If hasfocus=True Then
Send.Color=Colors.Cyan
Else
Send.Color=Colors.white
End If
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I have a layout created via the designer that has 10 views of the type EditText (not initialized),
Sorry, but as you have defined the EditText views with the Designer and if you load the layout file the EditText views are automaticaly initialized, you must not initialize them once more.
In the Designer you should set a same Event name for all EditTExt views. Then in the FocusChanged event you must use the Sender object to get which EditText view raised the event.

Best regards.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thank you for the tip Klaus. It is working for me now. The mistake I made was to keep the event name in the designer the same as the EditText name. All I had to do is change the event Name for each of the EditText views to 'MyEdit' instead of the EditText name.
Merci beaucoup
 
Upvote 0
Top