如何在Libgdx中支持中文输入

时间:2015-05-11 19:41:52

标签: android libgdx

我是LibGdx世界的新手。我想创建一个舞台,在舞台内,我想创建两个演员,一个是TextField,一个是按钮。我希望TextField能够支持中文输入,但我试过,文本字段输入没有反应。我错过了什么?任何人都可以看看,如果有任何问题让我知道。非常感谢!

这是我的代码:

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class textFieldLanguageTest implements ApplicationListener {

    private Stage stage;
    private BitmapFont myFont;
    private TextFieldStyle textFieldStyle;
    private TextField textField;
    private ImageButton btn_add;
    TextureRegion btnu;
    TextureRegion btnd;
    TextureAtlas  atlas;

    @Override
    public void create() {      
        stage = new Stage(720, 480, false);
        myFont = new BitmapFont(Gdx.files.internal("data/myfont.fnt"),
                  Gdx.files.internal("data/myfont.png"), false);
        this.setButton();
        this.setTextInput();
        this.setListener();

        stage.addActor(btn_add);
        stage.addActor(textField);

        Gdx.input.setInputProcessor(stage);
    }

    public void setButton(){
        atlas = new TextureAtlas(Gdx.files.internal("data/test.pack"));
        btnu = atlas.findRegion("addbtnu");
        btnd = atlas.findRegion("addbtnd");
        TextureRegionDrawable Btn_UP = new TextureRegionDrawable(btnu);
        TextureRegionDrawable Btn_DOWN = new TextureRegionDrawable(btnd);
        btn_add = new ImageButton(Btn_UP, Btn_DOWN);
        btn_add.setPosition(200, 100);
        btn_add.setSize(100, 50);
    }

    public void setTextInput(){
        textFieldStyle = new TextFieldStyle();
        textFieldStyle.font = myFont;
        textFieldStyle.fontColor = Color.BLACK;
        textField = new TextField("Input here", textFieldStyle);
        textField.setPosition(100,  400);
        textField.setSize(200, 60);
        textField.setMaxLength(50);
    }

    public void setListener(){
        btn_add.addListener(new InputListener(){

            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button) {
                // TODO Auto-generated method stub
                System.out.println("input text:" + textField.getText());
                return true;
            }

        });

        textField.setTextFieldListener(new TextFieldListener(){

            public void keyTyped (TextField textField, char key) {
                if (key == '\n') 
                    textField.getOnscreenKeyboard().show(false);
            }
        });
    }

    @Override
    public void dispose() {

    }

    @Override
    public void render() {      
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        stage.act();
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

1 个答案:

答案 0 :(得分:0)