"Gửi" ngầm sau khi nhấn Xong trên bàn phím ở EditText cuối cùng


96

Tôi đã sử dụng một số ứng dụng mà khi tôi điền tên người dùng, sau đó chuyển đến mật khẩu của mình, nếu tôi nhấn "Xong" trên bàn phím, biểu mẫu đăng nhập sẽ tự động được gửi mà tôi không cần phải nhấp vào nút gửi. Làm thế nào là điều này được thực hiện?



Câu trả lời:


185

Thử cái này:

Trong bố cục của bạn, hãy đặt / chỉnh sửa cái này:

<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" />

Trong hoạt động của bạn, hãy đặt điều này (ví dụ: trong 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;
     }
 });

Nơi submit_btnđược bạn nút gửi với handler onclick của bạn đính kèm.


15
submit_btn.performClick();đang đốt mắt tôi. Khéo léo? Tại sao không gọi phương thức gửi?
Laurent Meyer

28
@LaurentMeyer Mô phỏng đầu vào của người dùng thường tốt hơn là gọi trực tiếp logic cơ bản trong những tình huống này. Ví dụ: nút gửi hiện có thể bị vô hiệu hóa, do đó, performanceClick () sẽ không làm gì cả (như dự định), nhưng nếu bạn gọi phương thức gửi trực tiếp, trước tiên bạn phải kiểm tra xem nút này có bị tắt hay không. Nó cũng sẽ phát âm thanh "nhấp chuột" như thể nút đã được nhấn, v.v.
Extragorey

3
@LaurentMeyer Ý bạn là gì về giao diện người dùng nhạy cảm? Và 5 người trong 6 tháng qua, chắc chắn. Hãy cho họ thời gian và mọi người có lẽ cũng sẽ đồng ý với tôi. ;)
Extragorey

Hãy xem xét rằng bạn thay đổi giao diện người dùng, sử dụng nút cho việc khác. Mã sẽ là một mớ hỗn độn thực sự và thậm chí tệ hơn, bạn cần phải có quy trình kiểm tra thực sự rộng rãi để phát hiện loại lỗi đó. Thậm chí tệ hơn là khi bạn chia sẻ thành phần giao diện người dùng với các phương pháp như vậy.
Laurent Meyer

1
TWIMC, sử dụng imeActionLabeltrong EditText của tôi đã vô hiệu hóa tất cả hành vi này. Xem ra
Alwin Kesler

25

Bạn cần đặt Tùy chọn IME trên của mình 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" />

Sau đó, thêm một OnEditorActionListenervào dạng xem để nghe hành động "đã xong".

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;
    }
});

Tài liệu API chính thức: https://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent


22

Giải pháp đơn giản và hiệu quả với Kotlin

Mở rộng EditText:

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

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

       true

    }
}

Sau đó, sử dụng phương pháp mới như sau:

editText.onSubmit { submit() }

Đâu submit()là thứ như thế này:

fun submit() {
    // call to api
}

Phần mở rộng chung chung hơn

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

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

        true
    }
}

Và sau đó bạn có thể sử dụng nó để nghe sự kiện của mình:

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

6

đây là cách nó được thực hiện

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

Đừng quên thêm

<EditText android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:imeOptions="actionDone"/>

actionDone trong EditText của bạn.


2

Trong tệp XML bên trong thẻ edittext của bạn, hãy thêm đoạn mã bên dưới

android:imeOptions="actionDone"

Sau đó, bên trong lớp Java của bạn, hãy viết mã bên dưới

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { 


@Override 
  public boolean onEditorAction(TextView v, int id, KeyEvent event) { 
   if (id == EditorInfo.IME_ACTION_DONE) { 
      //do your work here 
      return true;
    } 

        return false; 
   } 
  });

1

thêm dòng sau vào văn bản chỉnh sửa

android:imeOptions="actionDone"

Viết mã vui vẻ


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

Đây chính xác là câu trả lời giống như câu trả lời này . Bạn nên giải thích một chút về cách bạn nghĩ điều này giải quyết vấn đề của OP.
Adrian W

1

Chỉ cần mở rộng câu trả lời này

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
        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" />

trong tệp .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;
        }
    });

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;
     }
 });
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.