android - Android 上的 libgdx 文本字段语言

标签 android input textfield libgdx

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

我的代码示例:

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

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

最佳答案

对于使用拉丁语和西里尔语等短字母的用户文本输入,您可以使用 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

关于android - Android 上的 libgdx 文本字段语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16097011/

相关文章:

android - 是否可以使用 webrtc 构建原生的 android 到 android 视频聊天应用程序?

android - Flutter Android 构建因 middles/compiled_local_resources/debug/out 失败,不是可读目录

java - 从用于 Google map 标记的 SQLite 数据库中读取值

jquery - 使用 Jqueryeach() 循环输入字段进行验证

textfield - Flutter Web - 文本字段滚动而不是选择长文本

cocoa - 如何为文本字段指定默认字体和一些填充?

android - 如何解决 IllegalStateException 和 ParserConfigurationException 以使用 apache OpenNLP 在 android studio 中查找位置

php - 使用 PHP echo 的 IP 地理编码

c - 仅打印长度超过 10 个字符的输入行 (ANSI C)

ios - 如何在同一 View 中编写多个 UIPickerView?