How to use a button to switch between EditText boxes?

rckb2

New Member
Licensed User
Longtime User
I have a special calculator with its own keypad that has 4 EditText boxes. I want to use a button to switch between the 4 EditText boxes. I'm new to B4Android and still learning. I appreciate the help from searching the forum, I'm very thankful! I've tried using RequestFocus but I'm missing something. When I touch the button it goes to the last RequestFocus(Length.RequestFocus).


Sub btnNext_Click
Wheel.RequestFocus
Thickness.RequestFocus
Stock.RequestFocus
Length.RequestFocus
End Sub

Thanks,

rckb2
 

Attachments

  • Slicing Calc ScreenShot.jpg
    Slicing Calc ScreenShot.jpg
    15.5 KB · Views: 342

kickaha

Well-Known Member
Licensed User
Longtime User
The sub is just doing what you have told it to do!

When it is excecuted it sets the focus to each one as asked, so it lands on the last one.

The way to do it would be to have a global EditText variable (lets call it NextFocus) set it to
B4X:
NextFocus = Wheel
when you start the app.

Now presuming that you want to cycle through them in the order given in your sub, you need this code:
B4X:
Sub Wheel_FocusChanged (HasFocus As Boolean)
If HasFocus Then NextFocus = Thickness
End Sub

Sub Thickness_FocusChanged  (HasFocus As Boolean)
If HasFocus Then NextFocus = Stock
End Sub

Sub Stock_FocusChanged  (HasFocus As Boolean)
If HasFocus Then NextFocus = Length
End Sub

Sub Length_FocusChanged  (HasFocus As Boolean)
If HasFocus Then NextFocus = Wheel
End Sub

Your button click then would be:
B4X:
Sub btnNext_Click
NextFocus.RequestFocus
End Sub

What happens is when an EditText get the focus, it sets NextFocus to point to the next EditText. When you press the button it sets the focus to the next one. This will fire the FocusChanged event of it, with HasFocus being true so the NextFocus variable will be updated ready for the next button press. You can still select the EditText manually, and the button will still work correctly.
 
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Setting Focus

Another option could be:


B4X:
Sub Globals
     Dim MyEditSel As Int : MyEditSel = 1
End Sub

Sub Btn_Click
     Select MyEditSel
          Case 1
       Wheel.RequestFocus
          Case 2
       Thickness.RequestFocus
          Case 3
       Stock.RequestFocus
          Case 4
       Length.RequestFocus
     End Select
     MyEditSel = MyEditSel + 1
     If MyEditSel > 4 Then
         MyEditSel = 1
     End If
End Sub
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Margret,

Your way is indeed very neat and it was my first thought, but I then realised that if an EditText was selected manually out of turn, pressing the "Next" button would not neccesarilly go to the next EditText, so I made mine more complex :cool:
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
EditText Focus

This is true but it would follow the order of the last field set with the button. It also allows you on a pause, resume to put the focus back where it was before the pause.

Of course you can always combine the two. Set the value of MyEditSel in the _FocusChanged Event.

You are the man!! Good to hear from you !!

Thanks,

Margret
 
Upvote 0

rckb2

New Member
Licensed User
Longtime User
Thank You Very Much,
Kickaha and Margret, This Forum is the BEST!

PHP:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
  
        Dim lblPitchSize As Label
   Dim lblQtyPerAmt As Label
   Dim Length As EditText
   Dim Stock As EditText
   Dim Thickness As EditText
   Dim Wheel As EditText
   Dim Number1,Number2,Number3,Number4,Number5,Number6 As Double
        Dim CurrentEdit As EditText
   Dim MyEditSel As Int : MyEditSel = 1   
End Sub

Sub Activity_Create(FirstTime As Boolean)
         Activity.LoadLayout("SlicingCalc")
    Wheel.Text=("0")
    Thickness.Text=("0")
    Stock.Text=("0")
    Length.Text=("1.000")
    CurrentEdit = Wheel
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnCalculate_Click
        Number1 = Length.Text
   Number2 = Thickness.Text
   Number3 = Wheel.Text
   Number4 = Stock.Text
   Number5 =  (Number2 + Number3 + Number4)
   Number6 =  Round2(Number1 / Number5,1)
        lblPitchSize.Text = Number5
         lblQtyPerAmt.Text = Number6
End Sub

Sub btnEvent_Click
    Dim btn As Button
    btn = Sender
    CurrentEdit.Text = CurrentEdit.Text & btn.Tag
End Sub    

Sub Wheel_FocusChanged (HasFocus As Boolean)
  If HasFocus Then CurrentEdit = Wheel
End Sub

Sub Thickness_FocusChanged (HasFocus As Boolean)
  If HasFocus Then CurrentEdit = Thickness
End Sub   

Sub Stock_FocusChanged (HasFocus As Boolean)
  If HasFocus Then CurrentEdit = Stock
End Sub   
Sub Length_FocusChanged (HasFocus As Boolean)
   If HasFocus Then CurrentEdit = Length
End Sub
Sub btnBS_Click
   If CurrentEdit.text.Length >0 Then
  CurrentEdit.Text = CurrentEdit.Text.SubString2(0,CurrentEdit.text.Length - 1)
End If
End Sub

Sub btnNext_Click 
  Select MyEditSel
          Case 1
       Wheel.RequestFocus
          Case 2
       Thickness.RequestFocus
          Case 3
       Stock.RequestFocus
          Case 4
       Length.RequestFocus
     End Select
     MyEditSel = MyEditSel + 1
     If MyEditSel > 4 Then
         MyEditSel = 1
     End If
End Sub
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Excellent, reading your code it seems that the examples given have clicked with what you want to do!
 
Upvote 0
Top