Android Question IF then else if question

chishen

New Member
If i want do {Then EditText3.Text="您輸入的不正確,請重新輸入" } *3 and ExitApplication
How should i do now?

Sub Button1_Click
Dim Username As String
Dim Password As String
Username = EditText1.Text
Password = EditText2.Text
T=3
If Username = "ABCDE" OR Password = "12345" Then
EditText3.Text="您是合法使用者!真厲害"
Else If Username = "" OR Password = "" Then
EditText3.Text="您輸入的不正確,請重新輸入"
' Else If T=T+1
'Else If T=3 Then ExitApplication
End If
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

Creating a log-in activity:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   For i = 1 To 3
     Wait For Button1_Click
     If EditText1.Text = "user" And EditText2.Text = "password" Then
       Log("success")
       StartActivity(Activity2)
       Exit
     Else
       ToastMessageShow("Wrong password", False)
     End If
   Next
   Activity.Finish
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
If i want do {Then EditText3.Text="您輸入的不正確,請重新輸入" } *3 and ExitApplication
How should i do now?

Sub Button1_Click
Dim Username As String
Dim Password As String
Username = EditText1.Text
Password = EditText2.Text
T=3
If Username = "ABCDE" OR Password = "12345" Then
EditText3.Text="您是合法使用者!真厲害"
Else If Username = "" OR Password = "" Then
EditText3.Text="您輸入的不正確,請重新輸入"
' Else If T=T+1
'Else If T=3 Then ExitApplication
End If
End Sub
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Num_of_Insert As Int = 0
End Sub

Sub Button1_Click
    Dim Username As String = EditText1.Text
    Dim Password As String = EditText2.Text
 
    Num_of_Insert=Num_of_Insert+1
    If Username = "ABCDE" Or Password = "12345" Then
        'Right
        EditText3.Text="您是合法使用者!真厲害"
    Else If Username = "" Or Password = "" Then
        'Empty
        EditText3.Text="您輸入的不正確,請重新輸入"
    Else
        'Wrong password
        If Num_of_Insert= 3 Then ExitApplication
    End If
End Sub
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
If you don't want to count an empty username and password as a failed attempt, then (looking at @Star-Dust's code) move the Num_of_Insert increment into the Else portion of the If statement. Also, should there not be an And in the first If part instead of Or?
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Num_of_Insert As Int = 0
End Sub

Sub Button1_Click
    Dim Username As String = EditText1.Text
    Dim Password As String = EditText2.Text
 
    If Username = "ABCDE" And Password = "12345" Then
        'Right
        EditText3.Text="您是合法使用者!真厲害"
    Else If Username = "" Or Password = "" Then
        'Empty
        EditText3.Text="您輸入的不正確,請重新輸入"
    Else
        'Wrong password
        Num_of_Insert=Num_of_Insert+1
        If Num_of_Insert >= 3 Then ExitApplication
    End If
End Sub

But, if a blank username and/or password does count as an invalid try, then the above will never exit (nor @Star-Dust's) as long as one of them is blank as the user tries. The code then needs to be:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Num_of_Insert As Int = 0
End Sub

Sub Button1_Click
    Dim Username As String = EditText1.Text
    Dim Password As String = EditText2.Text
 
    If Username = "ABCDE" And Password = "12345" Then
        'Right
        EditText3.Text="您是合法使用者!真厲害"
    Else If Username = "" Or Password = "" Then
        'Empty
        Num_of_Insert=Num_of_Insert+1
        EditText3.Text="您輸入的不正確,請重新輸入"
    Else
        'Wrong password
        Num_of_Insert=Num_of_Insert+1
    End If
    If Num_of_Insert >= 3 Then ExitApplication

End Sub
 
Upvote 0
Top