अंतिम EditText पर कीबोर्ड पर डोन मारने के बाद "सबमिट" करें


98

मैंने कुछ एप्लिकेशन का उपयोग किया है, जहां मैं अपना उपयोगकर्ता नाम भरता हूं, फिर अपने पासवर्ड पर जाता हूं, अगर मैं कीबोर्ड पर "हो गया" हिट करता हूं, तो लॉगिन फॉर्म स्वचालित रूप से सबमिट किया जाता है, मेरे बिना सबमिट बटन पर क्लिक करने के लिए। यह कैसे किया जाता है?



डॉक्स के लिए त्वरित लिंक: इनपुट विधि क्रिया निर्दिष्ट करें
FirstOne

जवाबों:


187

इसे इस्तेमाल करे:

अपने लेआउट में इसे डालें / संपादित करें:

<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आपके ओनलीक हैंडलर के साथ आपका सबमिट बटन कहां संलग्न है।


15
submit_btn.performClick();मेरी आँखों में जलन है। Srsly? सबमिट विधि को क्यों नहीं कहते हैं?
लॉरेंट मेयर

28
@LaurentMeyer उपयोगकर्ता इनपुट का अनुकरण आमतौर पर इन स्थितियों में अंतर्निहित तर्क को सीधे कॉल करने से बेहतर है। उदाहरण के लिए, सबमिट बटन को वर्तमान में अक्षम किया जा सकता है, इसलिए PerformClick () कुछ भी नहीं करेगा (जैसा कि इरादा है), लेकिन अगर आप सीधे सबमिट विधि कहते हैं, तो आपको यह जांचना होगा कि बटन पहले अक्षम नहीं था। यह "क्लिक" ध्वनि भी
बजाएगा

3
@LaurentMeyer यूआई संवेदनशील द्वारा आपका क्या मतलब है? और पिछले 6 महीनों में 5 लोग, निश्चित रूप से। उन्हें समय दें और लोग शायद मुझसे भी सहमत होंगे। ;)
एक्सट्रैगोरेय

आइए विचार करें कि आप यूआई को बदलते हैं, जो किसी और चीज़ के लिए बटन का उपयोग करते हैं। कोड एक वास्तविक गड़बड़ी होगी और इससे भी बदतर, आपको उस तरह के बग का पता लगाने के लिए वास्तव में व्यापक परीक्षण प्रक्रियाएं करने की आवश्यकता है। इससे भी बदतर है जब आप यूआई घटक को ऐसी प्रथाओं के साथ साझा करते हैं।
लॉरेंट मेयर

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

आधिकारिक एपीआई डॉक: https://developer.android.com/guide/topics/ui/controls/text.html#stationventvent


22

कोटलिन के साथ सरल और प्रभावी समाधान

विस्तार 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"/>

अपने EditText में कार्रवाई करें।


2

अपने एक्सटेक्स्ट टैग के अंदर आपकी XML फ़ाइल में स्निपेट के नीचे जोड़ें

android:imeOptions="actionDone"

फिर अपने जावा क्लास के अंदर, नीचे दिए गए कोड को लिखें

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

यह वास्तव में इस एक के रूप में एक ही जवाब है । आपको थोड़ा समझाना चाहिए कि आपको कैसे लगता है कि यह ओपी की समस्या को हल करता है।
एड्रियन डब्ल्यू

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

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;
     }
 });
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.