2D Toolkit Forum

2D Toolkit => Support => Topic started by: Jix on August 28, 2014, 04:36:13 am

Title: How do I adjust tk2dUITextInput to accept numbers only?
Post by: Jix on August 28, 2014, 04:36:13 am
Hello, I wrote a function to let tk2dUITextInput take only numbers from the users because this important feature isn't included in 2DToolkit. I want it to show only numbers so I made it that when the text input changes it will call this function
Code: [Select]
    void Check_UI_Input(tk2dUITextInput input)
    {
        string txt = "";
        for (int i = 0; i < input.Text.Length; i++)
        {
            if (char.IsDigit(input.Text[i]))
                txt += input.Text[i];
        }
        if (txt.Length > 0)
        {
            int number = int.Parse(txt);
            input.Text = number >= 0 ? number.ToString() : "0";
        }
        else
            input.Text = "";
    }

It works well in the Editor but when I deployed it on Android everything got messed up
Title: Re: How do I adjust tk2dUITextInput to accept numbers only?
Post by: unikronsoftware on August 28, 2014, 11:50:49 am
On android it uses the built in text input system, you'd probably need to modify this line -
keyboard = TouchScreenKeyboard.Open(text, TouchScreenKeyboardType.Default, false, false, false, false);

to

keyboard = TouchScreenKeyboard.Open(text, TouchScreenKeyboardType.NumberPad, false, false, false, false);
Title: Re: How do I adjust tk2dUITextInput to accept numbers only?
Post by: Jix on August 31, 2014, 10:49:32 pm
Thanks, does this problem also appear on iOS?
Title: Re: How do I adjust tk2dUITextInput to accept numbers only?
Post by: unikronsoftware on September 01, 2014, 11:17:52 am
No idea. They behave differently on all platforms supporting the touchscreen keyboard, but I cant remember how each platform works. You're best off adding the option I mentioned so you're filtering stuff off at the keyboard itself.
Title: Re: How do I adjust tk2dUITextInput to accept numbers only?
Post by: Jix on September 04, 2014, 11:40:40 pm
Ok, thanks a lot :-)