เป็นไปได้หรือไม่ที่จะตรวจจับเมื่อกดDoneปุ่มของแป้นพิมพ์บนหน้าจอ?
เป็นไปได้หรือไม่ที่จะตรวจจับเมื่อกดDoneปุ่มของแป้นพิมพ์บนหน้าจอ?
คำตอบ:
ใช่มันเป็นไปได้:
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;
ข้อมูลบรรณาธิการเป็นคลาสที่มีประโยชน์ที่สุดเมื่อคุณต้องจัดการกับข้อมูลป้อนเข้าของผู้ใช้ทุกประเภทในแอปพลิเคชัน Android ของคุณ เช่นในการเข้าสู่ระบบ / การลงทะเบียน / การค้นหาเราสามารถใช้เพื่อป้อนข้อมูลแป้นพิมพ์ที่แม่นยำยิ่งขึ้น คลาสข้อมูลตัวแก้ไขอธิบายแอตทริบิวต์ต่างๆสำหรับอ็อบเจ็กต์การแก้ไขข้อความซึ่งวิธีการป้อนข้อมูลจะสื่อสารโดยตรงกับเนื้อหาแก้ไขข้อความ
คุณสามารถลองกับIME_ACTION_DONE
การดำเนินการนี้ดำเนินการโดยDoneไม่มีการป้อนข้อมูลใด ๆ และระบบIMEจะปิด
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;
}
});
การใช้ 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;
}
EditorInfo.IME_ACTION_SEARCHปุ่มค้นหาเลนส์แทน