GameTextInput   เป็นส่วนหนึ่งของ Android Game Development Kit

การใช้ไลบรารี GameTextInput เป็นทางเลือกที่ง่ายกว่าในการเขียนแอป Android แบบเต็มหน้าจอที่ใช้แป้นพิมพ์ซอฟต์แวร์สำหรับการป้อนข้อความ

GameTextInput มี API ที่ตรงไปตรงมาเพื่อแสดงหรือซ่อนคีย์บอร์ดเสมือน ตั้งค่าหรือรับข้อความที่กำลังแก้ไข และรับการแจ้งเตือนเมื่อ ข้อความมีการเปลี่ยนแปลง โดยไม่ได้มีไว้สำหรับแอปโปรแกรมแก้ไขข้อความที่สมบูรณ์ แต่ยังคงรองรับการเลือกและ การเขียนในภูมิภาคสำหรับกรณีการใช้งานทั่วไปในเกม นอกจากนี้ ไลบรารียังรองรับฟีเจอร์ตัวแก้ไขวิธีการป้อนข้อมูล (IME) ขั้นสูง เช่น การตรวจตัวสะกด การเติมข้อความ และอักขระหลายคีย์

ภายใน GameTextInput จะสะสมข้อความที่ป้อน (พร้อมกับสถานะที่เกี่ยวข้อง) ไว้ในบัฟเฟอร์ภายใน GameTextInput::currentState_ และแจ้งให้แอปทราบหากมีการเปลี่ยนแปลงใดๆ จากนั้นแอปจะประมวลผลข้อความในฟังก์ชัน Callback ที่ลงทะเบียนไว้

ความพร้อมใช้งาน

GameTextInput สามารถใช้ได้ในลักษณะต่อไปนี้

  • ร่วมกับ GameActivity: GameActivity ผสานรวม GameTextInput แอปพลิเคชันที่ใช้ GameActivity จะใช้ได้เฉพาะ GameTextInput ที่ผสานรวมแล้วเท่านั้น วิธีการใช้งานมีการจัดทำเป็นเอกสารไว้อย่างครบถ้วนในหน้า GameActivity ดูตัวอย่างการผสานรวม GameActivity และ GameTextInput ได้ที่ที่เก็บ games-samples รูปแบบการใช้งานนี้ ไม่อยู่ในขอบเขตของคู่มือนี้

  • ในฐานะไลบรารีแบบสแตนด์อโลน: ส่วนที่เหลือของคู่มือจะอธิบายขั้นตอนการใช้งาน

โปรดทราบว่า 2 วิธีข้างต้นไม่สามารถใช้ร่วมกันได้

GameTextInputรุ่นที่เผยแพร่อย่างเป็นทางการมีอยู่ในไลบรารีเกม Jetpack ในMaven ของ Google

ตั้งค่าบิลด์

GameTextInput จัดจำหน่ายในรูปแบบ Android Archive (AAR) AAR นี้มีคลาส Java และ ซอร์สโค้ด C ซึ่งใช้ฟีเจอร์เนทีฟของ GameTextInput คุณต้องรวมไฟล์ต้นฉบับเหล่านี้เป็นส่วนหนึ่งของกระบวนการบิลด์ผ่าน Prefab ซึ่งจะแสดงไลบรารีแบบเนทีฟและซอร์สโค้ดต่อโปรเจ็กต์ CMake หรือบิลด์ NDK

  1. ทำตามวิธีการในหน้า Jetpack Android Games เพื่อเพิ่มทรัพยากร Dependency ของไลบรารี GameTextInput ลงในไฟล์ build.gradle ของเกม โปรดทราบ ว่าหากแอปพลิเคชันของคุณใช้ GameActivity แอปจะใช้ไลบรารี GameTextInput แบบสแตนด์อโลนไม่ได้

  2. ตรวจสอบว่า gradle.properties มีบรรทัดต่อไปนี้

    # Tell Android Studio we are using AndroidX. android.useAndroidX=true # Use Prefab 1.1.2 or higher, which contains a fix for "header only" libs. android.prefabVersion=1.1.2 # Required only if you're using Android Studio 4.0 (4.1 is recommended). # android.enablePrefab=true 
  3. นำเข้าแพ็กเกจ game-text-input แล้วเพิ่มลงในเป้าหมายในไฟล์ CMakeLists.txt ของโปรเจ็กต์

    find_package(game-text-input REQUIRED CONFIG) ... target_link_libraries(... game-text-input::game-text-input) 
  4. ในไฟล์ .cpp ไฟล์ใดไฟล์หนึ่งในเกม ให้เพิ่มบรรทัดต่อไปนี้เพื่อรวมการติดตั้งใช้งาน GameTextInput

    #include <game-text-input/gametextinput.cpp> 
  5. ในไฟล์ต้นฉบับที่ใช้ GameTextInput C API ให้รวมไฟล์ส่วนหัวต่อไปนี้

    #include <game-text-input/gametextinput.h> 
  6. คอมไพล์และเรียกใช้แอป หากพบข้อผิดพลาดของ CMake ให้ตรวจสอบว่าได้ตั้งค่า AAR และไฟล์ build.gradle อย่างถูกต้อง หากไม่พบไฟล์ #include ให้ตรวจสอบไฟล์การกำหนดค่า CMakeLists.txt

ผสานรวมบิลด์

  1. จากเธรด C ที่แนบกับ JVM อยู่แล้ว หรือเธรดหลักของแอป ให้เรียกใช้ GameTextInput_init ด้วยตัวชี้ JNIEnv

    static GameTextInput* gameTextInput = nullptr;  extern "C" JNIEXPORT void JNICALL Java_com_gametextinput_testbed_MainActivity_onCreated(JNIEnv* env,   jobject this) { {     if(!gameTextInput)       gameTextInput = GameTextInput_init(env);     ... } 
  2. สร้างInputEnabledTextViewคลาส Java ที่มีสิทธิ์เข้าถึง InputConnection

    public class InputEnabledTextView extends View implements Listener {   public InputConnection mInputConnection;   public InputEnabledTextView(Context context, AttributeSet attrs) {     super(context, attrs);   }    public InputEnabledTextView(Context context) {     super(context);   }   public void createInputConnection(int inputType) {     EditorInfo editorInfo = new EditorInfo();     editorInfo.inputType = inputType;     editorInfo.actionId = IME_ACTION_NONE;     editorInfo.imeOptions = IME_FLAG_NO_FULLSCREEN;     mInputConnection = new InputConnection(this.getContext(), this,             new Settings(editorInfo, true)     ).setListener(this);   }    @Override   public InputConnection onCreateInputConnection(EditorInfo outAttrs) {     if (outAttrs != null) {         GameTextInput.copyEditorInfo(mInputConnection.getEditorInfo(), outAttrs);     }     return mInputConnection;   }    // Called when the IME input changes.   @Override   public void stateChanged(State newState, boolean dismissed) {     onTextInputEventNative(newState);   }   @Override   public void onImeInsetsChanged(Insets insets) {     // handle Inset changes here   }    private native void onTextInputEventNative(State softKeyboardEvent); } 
  3. เพิ่ม InputEnabledTextView ที่สร้างขึ้นไปยังเลย์เอาต์ UI เช่น โค้ดต่อไปนี้ใน activity_main.xml จะวางไว้ที่ด้านล่างของ หน้าจอได้

    <com.android.example.gametextinputjava.InputEnabledTextView     android:id="@+id/input_enabled_text_view"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     app:layout_constraintBottom_toBottomOf="parent"     app:layout_constraintEnd_toEndOf="parent"     app:layout_constraintStart_toStartOf="parent" /> 
  4. เรียกข้อมูลคลาส InputEnabledTextView ใหม่นี้ไปยังกิจกรรม Java ซึ่งจะ ค่อนข้างง่ายเมื่อใช้ View Binding

    public class MainActivity extends AppCompatActivity {   ...   private ActivityMainBinding binding;   private InputEnabledTextView inputEnabledTextView;    private native void setInputConnectionNative(InputConnection c);    @Override   protected void onCreate(Bundle savedInstanceState) {     ...     binding = ActivityMainBinding.inflate(getLayoutInflater());     inputEnabledTextView = binding.inputEnabledTextView;     inputEnabledTextView.createInputConnection(InputType.TYPE_CLASS_TEXT);     setInputConnectionNative(inputEnabledTextView.mInputConnection);   } 
  5. ในไลบรารี C ให้ส่ง inputConnection ไปยัง GameTextInput_setInputConnection ส่งการเรียกกลับใน GameTextInput_setEventCallback เพื่อรับการแจ้งเตือนเหตุการณ์เป็นโครงสร้างสถานะ C GameTextInputState

    extern "C"JNIEXPORT void JNICALL Java_com_gametextinput_testbed_MainActivity_setInputConnectionNative(   JNIEnv *env, jobject this, jobject inputConnection) {   GameTextInput_setInputConnection(gameTextInput, inputConnection);   GameTextInput_setEventCallback(gameTextInput,[](void *ctx, const GameTexgtInputState *state) {     if (!env || !state) return;     // process the newly arrived text input from user.     __android_log_print(ANDROID_LOG_INFO, "TheGreateGameTextInput", state->text_UTF8);   }, env); } 
  6. ในไลบรารี C ให้เรียกใช้ GameTextInput_processEvent ซึ่งจะเรียกใช้การเรียกกลับที่ลงทะเบียนไว้ในขั้นตอนก่อนหน้าภายใน เพื่อให้ แอปจัดการเหตุการณ์เมื่อสถานะเปลี่ยนแปลง

    extern "C" JNIEXPORT void JNICALL Java_com_gametextinput_testbed_InputEnabledTextView_onTextInputEventNative(   JNIEnv* env, jobject this, jobject soft_keyboard_event) {   GameTextInput_processEvent(gameTextInput, soft_keyboard_event); } 

ฟังก์ชันยูทิลิตี

GameTextInput ไลบรารีมีฟังก์ชันยูทิลิตีที่ช่วยให้คุณแปลง ระหว่างออบเจ็กต์สถานะ Java และโครงสร้างสถานะ C ได้ เข้าถึงฟังก์ชันการทำงานสำหรับการแสดง และซ่อน IME ผ่านฟังก์ชัน GameTextInput_showIme และ GameTextInput_hideIme

ข้อมูลอ้างอิง

นักพัฒนาแอปอาจพบว่าข้อมูลต่อไปนี้มีประโยชน์เมื่อสร้างแอปด้วย GameTextInput

ความคิดเห็น

หากพบปัญหาและมีข้อสงสัยเกี่ยวกับ GameTextInput โปรดสร้างข้อบกพร่องใน Google IssueTracker