while(true){// Read all pending events.intevents;structandroid_poll_source*source;while((ALooper_pollOnce(engine.animating?0:-1,nullptr,&events,(void**)&source))>=0){// Process this event, etc....// Check if we are exiting.if(app->destroyRequested!=0){engine_term_display(&engine);return;}}engine_handle_input(app);// Process text input events if there is any outstanding.if(app->textInputState){// process TextInput events....//reset the textInputState flagapp->textInputState=0;}if(engine.animating){// draw frames.}}
extern"C"voidGameTextInputGetStateCB(void*ctx,conststructGameTextInputState*state){auto*engine=(structengine*)ctx;if(!engine||!state)return;// Process the text event(s).LOGI("UserInputText: %s",state->text_UTF8);// Clear the text input flag.engine->app->textInputState=0;}
在上一部分所示的游戏循环中,使用上述文本输入处理程序检查和处理文本:
if(state->textInputState){GameActivity_getTextInputState(app->activity,GameTextInputGetStateCB,// App's event handler shown above.&engine// Context to the GameTextInputGetStateCB function.);}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-26。"],[],[],null,["TextInput in GameActivity\nPart of [Android Game Development Kit](/games/agdk/overview).\n\n[GameActivity](/reference/games/game-activity) integrates\n[GameTextInput](/reference/games/game-text-input) by:\n\n- providing a wrapper\n- creating a flag for new text input event availability\n- directly using GameTextInput's state buffer for the text content\n\nAs shown in the following diagram, applications use different internal logical\ncomponents for user text input purpose:\n\n| **Note:** Bypassing GameActivity and directly creating a `GameTextInput` instance is out of the scope of this document. For more information about that usage, refer to the [GameTextInput documentation](/games/agdk/add-support-for-text-input).\n\nThere are three broad steps to using the built-in `GameTextInput` library:\n\n- Controlling the soft keyboard on UI\n- Knowing when new text is available\n- Retrieving the user input text and its states\n\nThe rest of this document describes them in detail. For an example of\n`GameTextInput` with `GameActivity` in action, see the\n[games-samples repository](https://github.com/android/games-samples).\n\nControl the soft keyboard on UI\n\n`GameActivity` provides two functions to control the soft keyboard on the UI:\n\n- [`GameActivity_showSoftInput()`](/reference/games/game-activity/group/game-activity#gameactivity_showsoftinput) displays the soft keyboard.\n- [`GameActivity_hideSoftInput()`](/reference/games/game-activity/group/game-activity#gameactivity_hidesoftinput) hides the soft keyboard.\n\nRefer to the API reference docs for their definitions. After the\nkeyboard is displayed, the application's UI may look similar to the following:\n\nCheck for text availability\n\nSoft keyboard events get passed from `GameTextInput` on the Java side to the\nC/C++ side through the JNI, then travel up to GameActivity's wrapper, finally\nreflecting in the [`android_app::textInputState`](/reference/games/game-activity/structandroid/app#textinputstate) flag\nimplemented in [`native_app_glue`](/reference/games/game-activity/group/android-native-app-glue). Applications\nshould poll this flag periodically to perform the intended processing:\n\n- GameActivity only sets the `android_app::textInputState` flag.\n- Applications poll the flag and handle the new `GameTextInput` events, such as the new text added to the input buffer.\n- Applications clear the `android_app::textInputState`.\n\nNote that `android_app::textInputState` does not differentiate between single\nand multiple text input events.\n\nFor a simple example, the following code polls the `textInputState` flag after\nhandling app cycle commands, touch events, and key events: \n\n while (true) {\n // Read all pending events.\n int events;\n struct android_poll_source* source;\n\n while ((ALooper_pollOnce(engine.animating ? 0 : -1, nullptr, &events,\n (void**)&source)) \u003e= 0) {\n // Process this event, etc.\n ...\n // Check if we are exiting.\n if (app-\u003edestroyRequested != 0) {\n engine_term_display(&engine);\n return;\n }\n }\n engine_handle_input(app);\n\n // Process text input events if there is any outstanding.\n if (app-\u003etextInputState) {\n // process TextInput events.\n ...\n //reset the textInputState flag\n app-\u003etextInputState = 0;\n }\n if (engine.animating) {\n // draw frames.\n }\n }\n\nRetrieve the user input text\n\nThe input texts and other states are accumulated in GameTextInput's\ninternal buffer, [`GameTextInput::currentState_`](/reference/games/game-text-input/struct/game-text-input-state). Applications\ncan use one of the following ways to retrieve its content:\n\n- GameActivity's wrapper API (recommended)\n- GameTextInput API\n\nGet TextInput state with GameActivity API\n\nApplications acquire the current text input with the typical callback mechanism:\n\n- Implement a callback function of type [`GameTextInputGetStateCallback`](/reference/games/game-text-input/group/game-text-input#gametextinputgetstatecallback) to process text input events.\n- Call `GameActivity_getInputState()` when there is one or multiple outstanding events.\n- Clear the [`android_app::textInputState`](/reference/games/game-activity/structandroid/app#textinputstate) after the events are processed.\n\nContinuing with the snippet in the previous section, the following code\nacquires a reference to the text input buffer, processes it (not shown), and\nresets the event flag: \n\n extern \"C\" void GameTextInputGetStateCB(void *ctx, const struct GameTextInputState *state) {\n auto* engine = (struct engine*)ctx;\n if (!engine || !state) return;\n\n // Process the text event(s).\n LOGI(\"UserInputText: %s\", state-\u003etext_UTF8);\n\n // Clear the text input flag.\n engine-\u003eapp-\u003etextInputState = 0;\n }\n\nIn the game loop shown in the previous section, check and process text\nwith the above text input handler: \n\n if (state-\u003etextInputState) {\n GameActivity_getTextInputState(\n app-\u003eactivity,\n GameTextInputGetStateCB, // App's event handler shown above.\n &engine // Context to the GameTextInputGetStateCB function.\n );\n }\n\nApplications can optionally initialize the `GameTextInputState` content with\n[`GameActivity_setTextInputState()`](/reference/games/game-activity/group/game-activity#gameactivity_settextinputstate).\n\nGet TextInput state with GameTextInput API\n\nApplications can also directly use `GameTextInput` API to retrieve the current\n`GameTextInputState`:\n\n- Use [`GameActivity_getTextInput()`](/reference/games/game-activity/group/game-activity#gameactivity_gettextinput) to get GameActivity's internal `GameTextInput` instance.\n- With the `GameTextInput` instance in hand, call [`GameTextInput_getState()`](/reference/games/game-text-input/group/game-text-input#gametextinput_getstate) to get the same `GameTextInputState` content.\n\nAgain, note that applications should not initialize `GameTextInput`\ndirectly; `GameActivity` already does that during its initialization process.\n\nThe callback mechanism is the same as that used by the GameActivity's\n`GameActivity_getTextInputState()` function.\n\nReferences\n\nDevelopers might find the following resources helpful when creating\n`GameActivity` applications:\n\n- [GameActivity get started](/games/agdk/game-activity/get-started)\n- [GameTextInput user documentation](/games/agdk/add-support-for-text-input)\n- [agdkTunnel sample](https://github.com/android/games-samples/tree/main/agdk/agdktunnel)\n- [Jetpack reference documentation for GameActivity](/reference/games/game-activity)\n- [Jetpack reference documentation for GameTextInput](/reference/games/game-text-input)\n- [AGDK source code](https://android.googlesource.com/platform/frameworks/opt/gamesdk/+/refs/heads/master/game-activity/)\n\nFeedback\n\nGameActivity and GameTextInput are both part of Jetpack games library. For any\nissues and questions, create [a bug on the Google IssueTracker](https://issuetracker.google.com/issues/new?component=897320&pli=1&template=1456805)."]]