โดยปริยาย "ส่ง" หลังจากกดปุ่มเสร็จสิ้นบนแป้นพิมพ์ที่ EditText สุดท้าย


99

ฉันเคยใช้แอพบางตัวเมื่อฉันกรอกชื่อผู้ใช้ของฉันจากนั้นไปที่รหัสผ่านของฉันหากฉันกด "เสร็จสิ้น" บนแป้นพิมพ์แบบฟอร์มการเข้าสู่ระบบจะถูกส่งโดยอัตโนมัติโดยที่ฉันไม่ต้องคลิกปุ่มส่ง วิธีนี้ทำได้อย่างไร?



คำตอบ:


191

ลองสิ่งนี้:

ในเค้าโครงของคุณให้ใส่ / แก้ไขสิ่งนี้:

<EditText
    android:id="@+id/search_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:singleLine="true"
    android:imeOptions="actionDone" />

ในกิจกรรมของคุณให้ใส่สิ่งนี้ (เช่นใน onCreate):

 // your text box
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });

ไหนsubmit_btnเป็นปุ่มส่งของคุณด้วยการจัดการ onclick ของคุณที่แนบมา


15
submit_btn.performClick();แสบตา Srsly? ทำไมไม่เรียกวิธีการส่ง?
Laurent Meyer

28
@LaurentMeyer การจำลองอินพุตของผู้ใช้มักจะดีกว่าการเรียกใช้ตรรกะพื้นฐานโดยตรงในสถานการณ์เหล่านี้ ตัวอย่างเช่นปุ่มส่งอาจถูกปิดใช้งานในขณะนี้ดังนั้น performClick () จะไม่ทำอะไรเลย (ตามที่ตั้งใจไว้) แต่ถ้าคุณเรียกวิธีการส่งโดยตรงคุณจะต้องตรวจสอบว่าปุ่มนั้นไม่ได้ถูกปิดใช้งานก่อน นอกจากนี้ยังจะเล่นเสียง "คลิก" ราวกับว่ามีการแตะปุ่ม ฯลฯ
Extragorey

3
@LaurentMeyer UI ที่ละเอียดอ่อนหมายความว่าอย่างไร และ 5 คนในช่วง 6 เดือนนี้นั่นเอง ให้เวลาพวกเขาแล้วคนก็น่าจะเห็นด้วยกับฉันเช่นกัน ;)
Extragorey

ลองพิจารณาว่าคุณเปลี่ยน UI ซึ่งใช้ปุ่มสำหรับอย่างอื่น โค้ดจะยุ่งมากและยิ่งแย่ไปกว่านั้นคุณต้องมีขั้นตอนการทดสอบที่ครอบคลุมมาก ๆ เพื่อตรวจหาจุดบกพร่องนั้น ๆ ที่แย่กว่านั้นคือเมื่อคุณแชร์องค์ประกอบ UI กับแนวทางปฏิบัติดังกล่าว
Laurent Meyer

2
TWIMC โดยใช้imeActionLabelใน EditText ของฉันได้ปิดใช้งานพฤติกรรมทั้งหมดนี้ ระวัง
Alwin Kesler

25

คุณต้องตั้งค่าตัวเลือก IME ในEditTextไฟล์.

<EditText
    android:id="@+id/some_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Whatever"
    android:inputType="text"
    android:imeOptions="actionDone" />

จากนั้นเพิ่มOnEditorActionListenerมุมมองเพื่อฟังการดำเนินการ "เสร็จสิ้น"

EditText editText = (EditText) findViewById(R.id.some_view);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // TODO do something
            handled = true;
        }
        return handled;
    }
});

เอกสาร API อย่างเป็นทางการ: https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent


22

วิธีแก้ปัญหาที่ง่ายและมีประสิทธิภาพด้วย Kotlin

ขยายEditText:

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->

       if (actionId == EditorInfo.IME_ACTION_DONE) {
           func()
       }

       true

    }
}

จากนั้นใช้วิธีการใหม่ดังนี้:

editText.onSubmit { submit() }

submit()สิ่งนี้อยู่ที่ไหน:

fun submit() {
    // call to api
}

ส่วนขยายทั่วไปเพิ่มเติม

fun EditText.on(actionId: Int, func: () -> Unit) {
    setOnEditorActionListener { _, receivedActionId, _ ->

       if (actionId == receivedActionId) {
           func()
       }

        true
    }
}

จากนั้นคุณสามารถใช้เพื่อฟังกิจกรรมของคุณ:

email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })

6

นี่คือวิธีการทำ

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

อย่าลืมเพิ่มการติดตาม

<EditText android:layout_height="wrap_content"

    android:layout_width="wrap_content"

    android:imeOptions="actionDone"/>

actionDoneใน EditText ของคุณ


2

ในไฟล์ XML ของคุณภายในแท็กแก้ไขของคุณให้เพิ่มข้อมูลโค้ดด้านล่าง

android:imeOptions="actionDone"

จากนั้นภายในคลาส Java ของคุณให้เขียนโค้ดด้านล่าง

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

1

เพิ่มบรรทัดต่อไปนี้ใน edittext

android:imeOptions="actionDone"

มีความสุขในการเขียนโค้ด


1
etParola = (EditText) findViewById(R.id.etParola); 
 btnGiris = (Button) findViewById(R.id.btnGiris);
  etParola.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    btnGiris.performClick();
                    return true;
                }
                return false;
            }
        });

 and;


layout xml etParola
android:imeOptions="actionDone" add

ตรงนี้เป็นคำตอบเดียวกับคนนี้ คุณควรอธิบายเล็กน้อยว่าคุณคิดว่าวิธีนี้ช่วยแก้ปัญหาของ OP ได้อย่างไร
Adrian W

1

เพียงแค่ขยายคำตอบนี้

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            clearFocus() // if needed 
            hideKeyboard()
            func()
        }
        true
    }
}

fun EditText.hideKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
}

0
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// which is u had set a imeoption
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });

0
<EditText
    android:id="@+id/signinscr_userName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/userName"
    android:imeOptions="actionNext" />

<EditText
    android:id="@+id/signinscr_password"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/password"
    android:imeOptions="actionDone"
    android:inputType="textPassword" />

ในไฟล์ java

EditText userNameField = (EditText) findViewById(R.id.signinscr_userName);
EditText passwordField = (EditText) findViewById(R.id.signinscr_password);

passwordField.setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
        //Do your operation here.
        return false;
    }
});
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.