toby Well-Known Member Licensed User Longtime User Apr 2, 2020 #1 I have simple test app (attached) with 3 views (2 EditText and 1 button). I want to save EditText2's change, all at once after editing is completed, so I thought FocusChanged event is better than TextChanged event which is fired for every letter entered or deleted. After entering some text at EditText2 field, If I click on EditText1, EditText2_FocusChanged would be fired as expected. However if I click Button1 instead of EditText1, the event in question wouldn't be fired. Did I miss anything? Is there a workaround? Thanks in advance for your helps! Attachments FocusChanged.zip FocusChanged.zip 9.2 KB · Views: 614
I have simple test app (attached) with 3 views (2 EditText and 1 button). I want to save EditText2's change, all at once after editing is completed, so I thought FocusChanged event is better than TextChanged event which is fired for every letter entered or deleted. After entering some text at EditText2 field, If I click on EditText1, EditText2_FocusChanged would be fired as expected. However if I click Button1 instead of EditText1, the event in question wouldn't be fired. Did I miss anything? Is there a workaround? Thanks in advance for your helps!
LucaMs Expert Licensed User Longtime User Apr 2, 2020 #2 It happens because Buttons are not "focusable" by default. Use this method (select the Reflection library): B4X: Sub SetFocusable(Vw As View, Focusable As Boolean) Dim objRefl As Reflector objRefl.Target = Vw objRefl.RunMethod2("setFocusable", Focusable, "java.lang.boolean") objRefl.RunMethod2("setFocusableInTouchMode", Focusable, "java.lang.boolean") End Sub Call it after Activity.Loadlayout: SetFocusable(Button1, True) Upvote 0
It happens because Buttons are not "focusable" by default. Use this method (select the Reflection library): B4X: Sub SetFocusable(Vw As View, Focusable As Boolean) Dim objRefl As Reflector objRefl.Target = Vw objRefl.RunMethod2("setFocusable", Focusable, "java.lang.boolean") objRefl.RunMethod2("setFocusableInTouchMode", Focusable, "java.lang.boolean") End Sub Call it after Activity.Loadlayout: SetFocusable(Button1, True)
Erel B4X founder Staff member Licensed User Longtime User Apr 2, 2020 #3 Equivalent JavaObject based code: Code: Sub SetFocusable(Vw As View, Focusable As Boolean) Dim jo As JavaObject = Vw jo.RunMethod("setFocusable", Array(Focusable)) jo.RunMethod("setFocusableInTouchMode", Array(Focusable)) End Sub Upvote 0
Equivalent JavaObject based code: Code: Sub SetFocusable(Vw As View, Focusable As Boolean) Dim jo As JavaObject = Vw jo.RunMethod("setFocusable", Array(Focusable)) jo.RunMethod("setFocusableInTouchMode", Array(Focusable)) End Sub
toby Well-Known Member Licensed User Longtime User Apr 2, 2020 #4 Both solutions work. Thanks ;-) Upvote 0