Android Question How to predetermine the numeric keyboard in a Dialog?

Discussion in 'Android Questions' started by Sergio Castellari, Jul 11, 2019.

  1. Sergio Castellari

    Sergio Castellari Member Licensed User

    Hello people,

    I have the following code that uses a B4XTable to 'load' NUMERIC values in some columns:
    Code:
    'Proceso la carga...
       If column.Id = "Energía" Or column.Id = "Agua" Then
           InputTemplate.Text = value
           InputTemplate.lblTitle.Text = column.Id
           
    Wait For (Dialog.ShowTemplate(InputTemplate, "Aceptar""""Cancelar")) Complete (Result As Int)
           
    If Result = xui.DialogResponse_Positive Then
    This code shows on screen a data entry dialog and the 'Full keyboard' ... so if you enter 'letters' it produces an error when recording the information as it expects them to be numbers.

    Query:
    1) How can I activate ONLY the numeric keypad?
    2) or how you can 'control that Result is ONLY a numerical value and otherwise ... put ZERO or ask again

    Regards,
    Sergio
     
  2. emexes

    emexes Well-Known Member Licensed User

    I don't know about q1 (although the ide tips-as-you-type auto-help might reveal an InputTemplate property to select the keyboard type).

    For q2, you can use IsNumber to check whether a string can be cast/parsed to a number (well, a double - so watch out for large values):
    upload_2019-7-12_11-16-43.png
    so perhaps something like:
    Code:
    Do While True
        InputTemplate.Text = value
        InputTemplate.lblTitle.Text = column.Id
        
    Wait For (Dialog.ShowTemplate(InputTemplate, "Aceptar""""Cancelar")) Complete (Result As Int)
        
    If Result = xui.DialogResponse_Positive Then
            
    If IsNumber(InputTemplate.Text) Then
                
    Exit 'Do Loop
            End If
        
    End If
    Loop
    which, if the user enters an invalid number, will reset the initial/default entry to value and ask 'em again. I'm not sure what should happen if the user chooses Cancelar, so I've left that for you to think about ;-)
     
    Sergio Castellari likes this.
  3. emexes

    emexes Well-Known Member Licensed User

    A slightly shadier way to check that something is a valid number might be:
    Code:
    Try
        MyIntVariable = InputTemplate.Text

    Catch
        
    Log("Nice try, no cigar - that ain't no number")

    End Try
    but I get a bit nervous intentionally doing stuff that will likely generate an error - feels like I'm inviting trouble into my program.

    But it works when I test it, so... what could possibly go wrong?!?!
     
    Last edited: Jul 12, 2019
  4. udg

    udg Expert Licensed User

    Another way to check for a numeric-only string could be by using regex. You could even check whether the value falls in a specific range.
     
    Sergio Castellari likes this.
  5. Sergio Castellari

    Sergio Castellari Member Licensed User

    Hello @emexes !!!

    Thanks for your help!. Someone passed me a property that directly disables letters and ONLY allows positive numbers:

    Code:
    If column.Id = "Energía" Or column.Id = "Agua" Then
           InputTemplate.Text = value
           InputTemplate.lblTitle.Text = column.Id
           InputTemplate.ConfigureForNumbers(
    FalseFalse'AllowDecimals, AllowNegative (Decimales,Negativos)    '<<<<<<<<
           Wait For (Dialog.ShowTemplate(InputTemplate, "Aceptar""""Cancelar")) Complete (Result As Int)
           
    If Result = xui.DialogResponse_Positive Then
    With this I work perfect!

    Regards,
    Sergio
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice