Android ตรวจจับกดปุ่มเสร็จสิ้นสำหรับแป้นพิมพ์บนหน้าจอ


คำตอบ:


277

ใช่มันเป็นไปได้:

editText = (EditText) findViewById(R.id.edit_text);

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
        }
        return false;
    }
});

โปรดทราบว่าคุณจะต้องนำเข้าไลบรารีต่อไปนี้:

import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

ขอบคุณคุณให้จุดเริ่มต้นที่ดีแก่ฉัน ฉันต้องการใช้EditorInfo.IME_ACTION_SEARCHปุ่มค้นหาเลนส์แทน
TechNyquist

สวัสดีเป็นไปได้ไหมกับ ButterKnife @ mikeyaworski @ SzabolcsBerecz
Maulik Dodia

1
@MaulikDodia คุณสามารถใช้ Butterknife's @OnEditorAction ()
Ridcully

ขอบคุณ. ฉันจะอยู่กับมัน @ Ridcully
Maulik Dodia

3

ข้อมูลบรรณาธิการเป็นคลาสที่มีประโยชน์ที่สุดเมื่อคุณต้องจัดการกับข้อมูลป้อนเข้าของผู้ใช้ทุกประเภทในแอปพลิเคชัน Android ของคุณ เช่นในการเข้าสู่ระบบ / การลงทะเบียน / การค้นหาเราสามารถใช้เพื่อป้อนข้อมูลแป้นพิมพ์ที่แม่นยำยิ่งขึ้น คลาสข้อมูลตัวแก้ไขอธิบายแอตทริบิวต์ต่างๆสำหรับอ็อบเจ็กต์การแก้ไขข้อความซึ่งวิธีการป้อนข้อมูลจะสื่อสารโดยตรงกับเนื้อหาแก้ไขข้อความ

คุณสามารถลองกับIME_ACTION_DONE

การดำเนินการนี้ดำเนินการโดยDoneไม่มีการป้อนข้อมูลใด ๆ และระบบIMEจะปิด

ใช้setOnEditorActionListener

EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            /* Write your logic here that will be executed when user taps next button */
            handled = true;
        }
        return handled;
    }
});

จะจัดการปัญหาเดียวกันกับคีย์บอร์ดแบบกำหนดเองได้อย่างไร?
Gaju Kollur

0

การใช้ Butterknife คุณสามารถทำได้

@OnEditorAction(R.id.signInPasswordText)
boolean onEditorAction(TextView v, int actionId, KeyEvent event){
    if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
        /* Write your logic here that will be executed when user taps next button */
    }
    return false;
}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.