Mysteria

Wednesday, February 19, 2014

Handling inputs : Text



Number
Category
Translate
  
Next 

13

How? 

French
  
Common Inputs for the two platforms:

" Text input "

What we will do ?

To sum things up, I'll give you a source code of a small application that asks user to enter text and then display him a message.
If you have read Common Inputs for the two platforms 1st part you will have no trouble doing this application, so the best way is to try to do things for yourself and if it does not work it not big deal just you can look at the solution below.

« Good Luck »


Solution
The source code is divided into 3 parts as we have already explained in a previous tutorial.
The logical part contain:
package my.work.textinput;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;

import com.badlogic.gdx.Input.TextInputListener;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class InputHandling implements ApplicationListener {
      
       private SpriteBatch batch ;
       private BitmapFont font ;
      
       private String texteInitial;
       private String titreBoiteDialogue;
       private String message;
      
       private boolean afficher;
       @Override
       public void create() {
            
       texteInitial = "Your name, please";
             titreBoiteDialogue = "Bienvenu";
             afficher = false;
            
             batch = new SpriteBatch();
             font = new BitmapFont();
            
             Gdx.input.getTextInput(new TextInputListener() {
                   
                    @Override
                    public void input(String texteSaisi) {
                    message = "Hello, <"+texteSaisi+"> Welcome to the world of developers ";
                    afficher =true;
                    }
                   
                    @Override
                    public void canceled() {
                   message = "Good-bye, you have missed something";
                    afficher =true;
                    }
             }, titreBoiteDialogue, texteInitial);
       }

       @Override
       public void dispose() {
       }

       @Override
       public void pause() {
       }

       @Override
       public void render() {
            
             Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
              if(afficher)
              {
                     batch.begin();
              font.draw(batch, message, 10, 200);
              batch.end();
              }
       }

       @Override
       public void resize(int arg0, int arg1) {
       }

       @Override
       public void resume() {
       }
}

The Desktop part: it contains the launcher application code for Desktop
package my.work.textinput;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;

public class DesktopLauncher {

       public static void main(String[] args) {
            
             new LwjglApplication(new InputHandling() ,"Input Handling ",480,320, false);
       }
}

The Android part : it contains the launcher application code for Android
package com.example.inputtextandroid;


import com.badlogic.gdx.backends.android.AndroidApplication;

import android.os.Bundle;

public class MainActivity extends AndroidApplication {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             initialize(new InputHandling(), false);
       }
}
 
The logical part is in the project with desktop launcher application, unlike the Android launcher found in an apartment project. We talked about this in detail in a previous tutorial.

The result
* Here is the result on Desktop:



Enter your name and press OK:



If you press Cancel:



* Now here's the result on Android:
Here are screenshots of the apps on my smartphone
This application is only functional on the landscape content with that for now mode. To make the emulator in landscape mode (because by default it is on portrait mode), do:
Ctrl (which is on the left) + F11
KEYPAD_7: press the number 7 ensuring that the lamp is off numbers.


If I enter my name and I press OK

If I press the Return key



  
You can download the project to import here

It's already all over

You can download both Android and desktop projects to import into your workspace.
Congratulations we have seen how to enter text by the user of your application and how to display it on your screen. Regarding the text display we will have more detail in the following sections.


 <<  Previous             
 Next     >>

Main Menu   


Problems ?
If you have any comments or questions please post them in the comments section . Thank you for reading.

See also !

 Files with libgdx

2 comments:

  1. the method Gdx.input.getTextInput(); at the end should have quotes
    .. }, titreBoiteDialogue, texteInitial, " ");

    ReplyDelete
  2. Lucky Club Casino Site 2021 Review + Free Bonus
    Lucky Club Casino is one of the biggest online casino luckyclub.live sites in the world. The site offers a huge selection of slots and games to its players. The site is

    ReplyDelete