Italian Come intercettare un tasto su un TextField ?

amorosik

Expert
Licensed User
Ho un TextFieldche uso per inserire una data
Vorrei avviare la selezione dati, che dipende dalla data nel TextField, nel momento che premo Enter (oppure F10 o altra combinazione tasti)
Che istruzioni usare?
Sto parlando dell'equivalente codice contenuto negli eventi KeyUp / KeyDown / KeyPress del VB
In B4X como se fa' ?
 

giannimaione

Well-Known Member
Licensed User
Longtime User
in B4J ???? c'è una libreria sul forum..... dovrei avere qualche esempio; cerco e ti tengo informato
 

Sagenut

Expert
Licensed User
Longtime User
B4X:
Sub textfieldeventname_Action
    'Cose da fare quando viene premuto Enter
End Sub
La Sub viene richiamata alla pressione di Enter nel TextField.
 

ivanomonti

Expert
Licensed User
Longtime User
quando inserisci un tasto gli dai un nome dell'evento,,, evento_click e fine (ero rimasto a ciò) visto che il pulsante è un object la test field che lo ospita eredita tutti gli eventi dell'oggetto ospitato
 

Sagenut

Expert
Licensed User
Longtime User

amorosik

Expert
Licensed User
B4X:
Sub textfieldeventname_Action
    'Cose da fare quando viene premuto Enter
End Sub
La Sub viene richiamata alla pressione di Enter nel TextField.

Viene richiamata da TUTTE le pressioni di ogni tasto
Quindi anche se vado ad editare la data mi partirebbe sempre, non desiderato, l'evento _Action
Oppure avrei bisogno di capire il tasto premuto ed acchiappare solamente l'Enter o combinazione di tasti desiderata
Quindi la domanda potrebbe deviare in:
- usando l'evento TextField_Action come 'capire' che tasto (o combinazione tasti) e' stato premuto?
 

Sagenut

Expert
Licensed User
Longtime User
Viene richiamata da TUTTE le pressioni di ogni tasto
Quindi anche se vado ad editare la data mi partirebbe sempre, non desiderato, l'evento _Action
Oppure avrei bisogno di capire il tasto premuto ed acchiappare solamente l'Enter o combinazione di tasti desiderata
Quindi la domanda potrebbe deviare in:
- usando l'evento TextField_Action come 'capire' che tasto (o combinazione tasti) e' stato premuto?
ACTION viene richiamato solo alla pressione del tasto ENTER.
TEXTCHANGED viene richiamato ad ogni lettera.
 

amorosik

Expert
Licensed User
ACTION viene richiamato solo alla pressione del tasto ENTER.
TEXTCHANGED viene richiamato ad ogni lettera.

Si hai ragione
Hai scritto _Action ed io ho provato con _TextChanged
Molto carino l'esempio riportato sopra
Grazie mille
 

amorosik

Expert
Licensed User
ma perchè non mettere il suffisso della piattaforma nel titolo che almeno si capisce di cosa stiamo parlando?

Si meglio
Ma in questo caso che si tratta di istruzione comune ai quattro ambienti, cosa mettere?
B4X o niente ?
 

giannimaione

Well-Known Member
Licensed User
Longtime User
ma perchè non mettere il suffisso della piattaforma nel titolo che almeno si capisce di cosa stiamo parlando?
caro Ivano, "ogni limite ha la sua pazienza!" (Totò);
a questo punto ti viene voglia di scrivere:
A lavare la testa all’asino si perde acqua, tempo e sapone
 

ivanomonti

Expert
Licensed User
Longtime User
e tempo che non scrivo e vedo cose nuove ma non ho capito come inserire il button nella textfield, quindi ti propongo questo codice
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim pn As Pane
    Dim tf As TextField
    Dim bt As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    init
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub init

    pn.Initialize("")
    MainForm.RootPane.AddNode(pn,10,10,300,32)

    tf.Initialize("")
    tf.Text = "Demo"
    pn.AddNode(tf,0,0,200,32)

    bt.Initialize("bt")
    bt.Text = "Click"
    pn.AddNode(bt,201,0,50,32)

End Sub

Sub bt_action
    fx.Msgbox(MainForm,tf.Text,"demo")
End Sub
puoi farti una classe così, qui io ho minimizzato il codice ma puoi fare tanto come passare testo posizione per ogni elemento della classe Pane+Textfield+Button e a tuo piacere inserirla dove vuoi o ti fai anche una libreria
B4X:
Sub Class_Globals
    Private fx As JFX
    Private pn As Pane
    Private tf As TextField
    Private bt As Button
    Private event As String
    Private GO As Object
End Sub

Public Sub Initialize(Component As Object,View As Form, NameEvent As String)

    GO = Component
    event = NameEvent

    pn.Initialize("")
    View.RootPane.AddNode(pn,10,10,300,32)

    tf.Initialize("")
    pn.AddNode(tf,0,0,200,32)

    bt.Initialize("bt")
    bt.Text = "Click"
    pn.AddNode(bt,201,0,50,32)

End Sub

Sub bt_action
    CallSubDelayed(GO,event)
End Sub
e richiamarla in questo modo
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private fb As FieldButton
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    fb.Initialize(Me,MainForm,"bt")
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub bt
    fx.Msgbox(MainForm,"Demo","demo")
End Sub
qui facendo invio sul textfield selezionato parte un evento
B4X:
Sub Class_Globals
    Private fx As JFX
    Private pn As Pane
    Private tf As TextField
    Private bt As Button
    Private event As String
    Private GO As Object
End Sub

Public Sub Initialize(Component As Object,View As Form, NameEvent As String)
   
    GO = Component
    event = NameEvent
   
    pn.Initialize("")
    View.RootPane.AddNode(pn,10,10,300,32)
   
    tf.Initialize("tf")
    pn.AddNode(tf,0,0,200,32)
   
    bt.Initialize("bt")
    bt.Text = "Click"
    pn.AddNode(bt,200-52,2,50,28)
   
End Sub

Sub bt_action
    CallSubDelayed(GO,event)
End Sub

' eventi click (invio) della textfield
Sub tf_Action
    CallSubDelayed(GO,event)
End Sub

demo.png
 

Attachments

  • TextFieldButton.zip
    30.2 KB · Views: 195
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
In questa versione ti ho messo due controlli dove la classe viene fornita, sono al vecchio stile, disegno da codice ogni mia intefaccia, comunque in questa classe trovi passaggio parametri dell'oggetto left,top ecc ecc, se vuoi vedere il bottone e dove e se il bottone è visibile allora il tasto return della textfield viene isolato e se nel textfield è vuoto allora ti apre un alert

Non voglio insegnare nulla perchè sto cercando di ricordarmi come si scrive il codice... a sorsi di minuti della giornata

Class Main:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private fb As FieldButton
End Sub

Sub AppStart (Form1 As Form, Args() As String)
  
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
  
    fb.Initialize(Me,MainForm,"bt")
  
    fb.PaneHeight(55)
    fb.PaneWidth(300)
    fb.PaneTop(10)
    fb.PaneLeft(10)
  
    fb.ButtonVisible(False)
    fb.ButtonTitle("Demo")
    fb.ButtonInternal(True)
  
  
    fb.TextValue("Demo")
    fb.TextTag("Demo")
    fb.TextPrompt("Demo")
  
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub bt
    fx.Msgbox(MainForm,fb.ValueReturn,"Demo")
End Sub
qui trovi la classe con i vari controlli
Class FieldButton:
Sub Class_Globals
    Private fx As JFX
    Private pn As Pane
    Private tf As TextField
    Private bt As Button
    Private event As String
    Private go As Object
    Public ValueReturn As String
End Sub

Public Sub Initialize(Component As Object,View As Form, NameEvent As String)
  
    go = Component
    event = NameEvent
  
    pn.Initialize("")
    View.RootPane.AddNode(pn,10,10,300,32)
  
    tf.Initialize("tf")
    pn.AddNode(tf,0,0,200,32)
  
    bt.Initialize("bt")
    bt.Text = "Click"
    pn.AddNode(bt,200+1,0,50,32)
  
End Sub

public Sub PaneTop(value As Double)
    pn.Top =  value
End Sub

public Sub PaneLeft(value As Double)
    pn.Left = value
End Sub

public Sub PaneWidth(value As Double)
    pn.PrefWidth = value
End Sub

public Sub PaneHeight(value As Double)
    pn.PrefWidth = value
End Sub

Public Sub ButtonVisible(value As Boolean)
    bt.Visible = value
End Sub

Public Sub ButtonTitle(value As String)
    bt.Text = value
End Sub

public Sub ButtonInternal(value As Boolean)
    If value = True Then
        bt.Left = ( pn.GetNode(0).Left + pn.GetNode(0).PrefWidth) - ( pn.GetNode(1).PrefWidth + 2)
        bt.top = pn.GetNode(0).Top + 2
        bt.PrefHeight =  pn.GetNode(0).PrefHeight - 4
    Else
        bt.Left = pn.GetNode(0).Left + pn.GetNode(0).PrefWidth + 1
        bt.PrefHeight = pn.GetNode(0).PrefHeight
    End If
End Sub

Public Sub TextValue(value As String)
    tf.Text = value
End Sub

Public Sub TextTag(value As Object)
    tf.Tag = value
End Sub

Public Sub TextPrompt(value As String)
    tf.PromptText = value
End Sub

private Sub bt_action
    If tf.Text <> "" Then
        ValueReturn = tf.Text
        CallSubDelayed(go,event)
    Else
        fx.Msgbox(Null,"field value is empty and invalid","Alert")
    End If
End Sub

private Sub tf_Action
    If bt.Visible = True Then Return
    If tf.Text <> "" Then
        ValueReturn = tf.Text
        CallSubDelayed(go,event)
    Else
        fx.Msgbox(Null,"field value is empty and invalid","Alert")
    End If
End Sub
 

Attachments

  • TextFieldButton.zip
    52.5 KB · Views: 192

LucaMs

Expert
Licensed User
Longtime User
Ho un TextFieldche uso per inserire una data
Vorrei avviare la selezione dati, che dipende dalla data nel TextField, nel momento che premo Enter (oppure F10 o altra combinazione tasti)
Che istruzioni usare?
Ma perché usare una EditText per inserire una data? Ci sono molti "date picker", belli e comodi!
https://www.b4x.com/android/forum/pages/results/?query=xui+views+datepicker
https://www.b4x.com/android/forum/threads/b4x-xui-anotherdatepicker.85160/#content
https://www.b4x.com/android/forum/t...ss-platform-views-and-dialogs.100836/#content

("... nel momento in cui premo Enter...")
 

amorosik

Expert
Licensed User

udg

Expert
Licensed User
Longtime User
E' da considerare anche l'aspetto pratico oltre al (purtroppo) prevalente aspetto estetico. Se devo inserire una data di nascita tramite un date picker passo la giornata a tornare indietro di 50 anni, mentre tramite un qualunque edit finisco in due secondi.
 

udg

Expert
Licensed User
Longtime User
Vero, però nei vecchi (e solidi) programmi potevi "validare" l'input man mano. Ad esempio se erano ammissibili solo i mesi dispari, impedivi di immettere un mese pari. Idem per range di giorni o anni. Con il dp prendi per buono e poi controlli a posteriori. Un edittext (che non è che sponsorizzi o mi piaccia in modo particolare, anzi) ti permette di controllare in qualche modo l'input nel textchanged.
 
Top