Android Question How to design a button that works in a infinite loop?

Loh Theen Poh

New Member
Licensed User
Longtime User
Is there a way to make button work even though there is a loop working in the background? I want the program to allow the user to select the desired units by pressing the button, yet I need the program to check the values of edittext1 and edittext2 continuously. I'm grateful for any help.
 

Attachments

  • pt8.zip
    328.4 KB · Views: 171

LucaMs

Expert
Licensed User
Longtime User
If I did not understand it, id not you can use this instead of the loop:

B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    If New.Length > 0 AND EditText2.Text.Length > 0 Then
        Calculate
    End If
End Sub

Sub EditText2_TextChanged (Old As String, New As String)
    If New.Length > 0 AND EditText1.Text.Length > 0 Then
        Calculate
    End If
End Sub

But may be I did not understand it; i'm only looked at your code on the fly.

P.S. In a loop you can use the command DoEvents
 
Last edited:
Upvote 0

Loh Theen Poh

New Member
Licensed User
Longtime User
Thanks! However after I have input values into the edittext box, the buttons no longer work. Can you help me with this?
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
You shouldn't create endless loops, is will cause the app to crash. You should use events like explained above to catch the textChange
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I try to see if I can help you, I try to read your code again.

But I advise you to put comments in it and give meaningful names to variables and the views, otherwise you will not understand the code even yourself, when you reread a few time!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Well.

I have not figured out what you want to achieve.

All that I understand is that it is MegaOhms, KiloOhms and Ohms :)

What calculates "Calculate"? (name it as "CalcTotalPower", "CalcTotalDiscount"...)

In my example, Calculate is ran when both EditText1 and EditText2 contain values. Then?

What checks "Check1"? (rename it)

What counts Count?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Just as something to experiment with, you may want to try this:
B4X:
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 edittextall(3) As EditText
   Dim btnsall(3) As Button
   Dim units(3) As String =Array As String ("Ω","ΚΩ","ΜΩ")
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer.
   For i=0 To 2
     If i<2 Then
       edittextall(i).Initialize ("edittext")
       edittextall(i).InputType=edittextall(i).INPUT_TYPE_NUMBERS
     Else
       edittextall(i).Initialize ("edittextFinal")
       edittextall(i).InputType=edittextall(i).INPUT_TYPE_NONE
       edittextall(i).Enabled=False
     End If
     Activity.AddView (edittextall(i),0,i*20%y,50%x-1dip,20%y-1dip)
     btnsall(i).Initialize ("btnsall")
     btnsall(i).Text=units(0)
     btnsall(i).Tag=0
     Activity.AddView (btnsall(i),50%x,i*20%y,50%x-1dip,20%y-1dip)
   Next   
End Sub
   
Sub edittext_textChanged(old As String, new As String)
   Calculate
End Sub

 Sub EditTextFinal_FocusChanged (HasFocus As Boolean)
   If HasFocus Then
     edittextall(0).RequestFocus
   End If
End Sub
   
   
Sub Calculate
   If edittextall(0).Text.Length >0 AND edittextall(1).Text.Length >0 Then   
       Dim Val1 As Double, val2 As Double, Val3 As Double
     Val1 = edittextall(0).Text
     val2 = edittextall(1).Text
     Val3 = Val1 + val2
     edittextall(2).Text = Val3
   Else
     edittextall(2).Text="N/A"
   End If
End Sub

Sub btnsall_Click
   Dim count As Int, tempBtn As Button
   tempbtn=Sender
   count=tempBtn.Tag
   count=(count+1) Mod 3
   tempBtn.Text=units(count)
   tempBtn.Tag=count
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Me ne importa un tubo

Neolaureata
Sto cazzo di grondaia è ancora rotto
Ho buttato via la sveglia il giorno della mia laurea
Ma sento ancora questo tic tac dal fondo

Neolaureata
Prima uscivo ogni notte
Dopo ore e ore di studio
E di fatica a convincermi a studiare
Ora esco solo di giorno
Sto in giro ore e ore
E faccio fatica a convincermi che non sto sognando

Neolaureata
Senza timore e senza lode
Messa al muro come la grondaia
Sto aspettando che finisca il mio noioso tic tac

Just a poem for a recently graduated friend :)
mc73 you are a genius :).

Io non c'ho capito un tubo!

(Unfortunately I do not know how to translate the sentence above :D)

I can only write that it was a puzzle
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Non ho capito il finale della tua poesia: a me sembra qualcosa di pessimistico!
Mah, oramai ho finito le celluline grigie


I did not understand the ending of your poem, it seems to me something pessimistic!
Well, now I've finished the gray cells
 
Upvote 0
Top