【问题标题】:libgdx textfield language on AndroidAndroid 上的 libgdx 文本字段语言
【发布时间】:2013-04-19 04:06:18
【问题描述】:

如何在 Android 上以本地语言输入 libgdx TextField? 例如,我的意思是输入登录名或密码的能力。是的,我可以调用类似 Gdx.input.getTextInput 或原生 Android 对话框之类的东西,但这真的很糟糕。

我的代码示例:

TextField loginField = new TextField("",getSkin());
loginField.setText("sdfdfsdаыва");

字段中的文本使用setText() 绘制得很好,即皮肤字体包含所有必需的字形。但在 Android(模拟器和物理)上,我无法输入任何本机字符。

【问题讨论】:

    标签: android input textfield libgdx


    【解决方案1】:

    对于使用拉丁文和西里尔文等短字母的用户文本输入,您可以使用 LibGDX FreeTypeFontGenerator 和 FreeTypeFontParameter :

    private BitmapFont yourCustomFont;
    private FreeTypeFontGenerator generator;
    private FreeTypeFontParameter parameter;
    private TextFieldStyle textFieldStyle;
    private TextField textField;
    
    ...
    
    public void show(){
    
        ...
    
        // Font generating
    
        yourCustomFont = new BitmapFont();
        generator = new FreeTypeFontGenerator(
                Gdx.files.internal("yourFont.ttf"));
        parameter = new FreeTypeFontParameter();
    
        // Complete the following sequence with the letters you need(each only once)
        parameter.characters =
        "БбДдЄєabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUXYZ/&1234567890()[]#'\"";
    
        parameter.size = (Gdx.graphics.getHeight() / 20);
        yourCustomFont = generator.generateFont(parameter);
        generator.dispose(); //Don't forget to dispose the generator
    
        // TextFieldStyle
        textFieldStyle = new TextFieldStyle();
        textFieldStyle.font = yourCustomFont;
        textFieldStyle.fontColor = Color.BLACK;
    
        // TextField
        textField = new textField("",textFieldStyle);
        textField.setTextFieldListener(new TextFieldListener() {
    
             public void keyTyped(TextField textField, char key) {
    
            }
        });
    
        ...
    
    }
    

    对于使用中文、韩文或日文等复杂字母的用户文本输入,您可能需要使用 .fnt + .png 格式的预渲染字体。 为此,您可以使用 Hiero 或 BMFont 等软件。

    http://www.angelcode.com/products/bmfont/

    http://github.com/libgdx/libgdx/wiki/Hiero

    使用 Hiero 或 BMFont 生成的复杂字母(韩文、日文、中文...)是大文件,因此您可能需要使用 LibGdx AssetManager 加载它们。然后将字体与您的 TextFieldStyle 一起使用,方法与前面描述的相同。

    希望对你有帮助。

    编辑: 现在,LibGDX 可以动态渲染复杂的字形: https://github.com/libgdx/libgdx/wiki/Gdx-freetype

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多