Has anyone used Alibaba's Qwen artificial intelligence https://chat.qwenlm.ai/ ? I found it much better than Deepseek, I asked her to generate a code for me in B4XPages with integration with an API for searching postal codes in Brazil and it was practically all correct, I just had to send her a code so she could learn how to use wait for in http requests instead of using job_done. Then I asked her to create the layout via code without having to use desiger, I had to adjust some details for her to answer me as I needed, but it was very interesting.
Code B4XMainPage
Code B4XMainPage
B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
' B4XPage1.bas
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
' Declaração dos componentes visuais
Private btnConsultar As B4XView
Private txtCep As B4XView
Private lblResultado As B4XView
End Sub
Public Sub Initialize
' Inicializa a página
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
' Configura o layout principal
Root.Color = xui.Color_White
' Cria os componentes programaticamente
CreateComponents
End Sub
Private Sub CreateComponents
' Cria um painel para organizar os componentes
Dim panel As B4XView = xui.CreatePanel("")
panel.SetLayoutAnimated(0, 0, 0, Root.Width, Root.Height) ' Define o tamanho do painel
Root.AddView(panel, 0, 0, Root.Width, Root.Height)
' Margens e espaçamento
Dim margin As Int = 20dip
Dim topPosition As Int = margin
Dim componentHeight As Int = 60dip
Dim componentWidth As Int = Root.Width - 2 * margin
' Cria o EditText para o CEP
txtCep = CreateEditText("txtCep", "Digite o CEP", False)
txtCep.SetLayoutAnimated(0, margin, topPosition, componentWidth, componentHeight)
panel.AddView(txtCep, margin, topPosition, componentWidth, componentHeight)
topPosition = topPosition + componentHeight + margin
' Define o teclado do EditText como numérico
SetKeyboardNumeric(txtCep)
' Aplica estilos ao EditText usando SetColorAndBorder
txtCep.SetColorAndBorder(xui.Color_White, 2dip, xui.Color_Gray, 10dip) ' Fundo branco, borda cinza, cantos arredondados
txtCep.TextColor = xui.Color_ARGB(255, 50, 50, 50) ' Cor da fonte quase preta (sólida)
txtCep.Font = xui.CreateDefaultFont(18) ' Tamanho da fonte 18dip
' Cria o botão de consulta
btnConsultar = CreateButton("btnConsultar", "Consultar CEP")
btnConsultar.SetLayoutAnimated(0, margin, topPosition, componentWidth, componentHeight)
panel.AddView(btnConsultar, margin, topPosition, componentWidth, componentHeight)
topPosition = topPosition + componentHeight + margin
' Aplica estilos ao botão usando SetColorAndBorder
btnConsultar.SetColorAndBorder(xui.Color_Blue, 0, 0, 10dip) ' Fundo azul, sem borda, cantos arredondados
btnConsultar.TextColor = xui.Color_White ' Cor da fonte branca
btnConsultar.Font = xui.CreateDefaultBoldFont(18) ' Tamanho da fonte 18dip
' Cria o Label para exibir o resultado
lblResultado = CreateLabel("lblResultado", "")
lblResultado.SetLayoutAnimated(0, margin, topPosition, componentWidth, Root.Height - topPosition - margin)
lblResultado.TextColor = xui.Color_Black
lblResultado.Font = xui.CreateDefaultBoldFont(16)
panel.AddView(lblResultado, margin, topPosition, componentWidth, Root.Height - topPosition - margin)
End Sub
Private Sub CreateEditText(EventName As String, Hint As String, MultLine As Boolean) As B4XView
#If B4A
Dim et As EditText
et.Initialize(EventName)
et.Hint = Hint
et.SingleLine = Not(MultLine)
et.Padding = Array As Int (10dip, 10dip, 10dip, 10dip)
#Else If B4I
Dim et As TextField
et.Initialize(EventName)
et.HintText = Hint
#Else If B4J
Dim et As TextField
et.Initialize(EventName)
et.PromptText = Hint
#End If
Return et
End Sub
Private Sub CreateButton(EventName As String, Text As String) As B4XView
Dim btn As Button
btn.Initialize(EventName)
btn.Text = Text
Return btn
End Sub
Private Sub CreateLabel(EventName As String, Text As String) As B4XView
Dim lbl As Label
lbl.Initialize(EventName)
lbl.Text = Text
Return lbl
End Sub
Private Sub SetKeyboardNumeric(et As B4XView)
' Altera o tipo de teclado para numérico
#If B4A
Dim editText As EditText = et
editText.InputType = editText.INPUT_TYPE_NUMBERS ' Para Android
#Else If B4I
Dim textField As TextField = et
textField.KeyboardType = textField.KEYBOARD_TYPE_DECIMAL ' Para iOS
#Else If B4J
Dim textField As TextField = et
textField.InputType = textField.INPUT_TYPE_NUMBERS ' Para Desktop (B4J)
#End If
End Sub
Private Sub btnConsultar_Click
Dim cep As String = txtCep.Text.Trim
' Verifica se o CEP foi digitado corretamente
If cep.Length <> 8 Then
lblResultado.Text = "Por favor, digite um CEP válido com 8 dígitos."
Return
End If
' Desativa o botão para evitar múltiplas consultas
btnConsultar.Enabled = False
' Informa ao usuário que a pesquisa está em andamento
lblResultado.Text = "Pesquisando CEP..."
' Chama a função para consultar o CEP
ConsultarCEP(cep)
End Sub
Private Sub ConsultarCEP(cep As String)
Dim job As HttpJob
job.Initialize("", Me)
job.Download($"https://viacep.com.br/ws/${cep}/json/"$)
' Aguarda o retorno da requisição HTTP
Wait For (job) JobDone(job As HttpJob)
' Reativa o botão após a conclusão da requisição
btnConsultar.Enabled = True
If job.Success Then
Dim jsonString As String = job.GetString
' Converte a string JSON em um Map usando JSONParser
Dim parser As JSONParser
parser.Initialize(jsonString)
Dim response As Map = parser.NextObject
' Verifica se houve erro na consulta
If response.ContainsKey("erro") Then
lblResultado.Text = "CEP não encontrado."
Else
' Extrai os dados do endereço
Dim logradouro As String = response.Get("logradouro")
Dim bairro As String = response.Get("bairro")
Dim localidade As String = response.Get("localidade")
Dim uf As String = response.Get("uf")
' Exibe o endereço no Label
lblResultado.Text = $"${logradouro}, ${bairro}, ${localidade} - ${uf}"$
End If
Else
lblResultado.Text = "Erro ao consultar o CEP."
End If
job.Release
End Sub
Attachments
Last edited: