Android बटन को क्लिक करने के लिए कीबोर्ड पर बटन का उपयोग करें


131

मेरे ऐप में ओके मैं एक नंबर इनपुट करने के लिए उपयोगकर्ता के लिए एक क्षेत्र है। मेरे पास केवल सेट संख्या स्वीकार करने के लिए फ़ील्ड सेट है। जब उपयोगकर्ता फ़ील्ड पर क्लिक करता है तो वह कीबोर्ड लाता है। कीबोर्ड पर (ICS पर) एक बटन किया गया है। मैं अपने आवेदन में सबमिट बटन को ट्रिगर करने के लिए कीबोर्ड पर किए गए बटन के लिए चाहूंगा। मेरा कोड इस प्रकार है।

package com.michaelpeerman.probability;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class ProbabilityActivity extends Activity implements OnClickListener {

private Button submit;
ProgressDialog dialog;
int increment;
Thread background;
int heads = 0;
int tails = 0;

public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    setContentView(R.layout.main);
    submit = ((Button) findViewById(R.id.submit));
    submit.setOnClickListener(this);
}

public void onClick(View view) {
    increment = 1;
    dialog = new ProgressDialog(this);
    dialog.setCancelable(true);
    dialog.setMessage("Flipping Coin...");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);
    EditText max = (EditText) findViewById(R.id.number);
    int maximum = Integer.parseInt(max.getText().toString());
    dialog.setMax(maximum);
    dialog.show();
    dialog.setOnCancelListener(new OnCancelListener(){

          public void onCancel(DialogInterface dialog) {

              background.interrupt();
              TextView result = (TextView) findViewById(R.id.result);
                result.setText("heads : " + heads + "\ntails : " + tails);


          }});


    background = new Thread(new Runnable() {
        public void run() {
            heads=0;
            tails=0;
            for (int j = 0; !Thread.interrupted() && j < dialog.getMax(); j++) {
                int i = 1 + new Random().nextInt(2);
                if (i == 1)
                    heads++;
                if (i == 2)
                    tails++;
                progressHandler.sendMessage(progressHandler.obtainMessage());
            }
        }
    });
    background.start();
}

Handler progressHandler = new Handler() {
    public void handleMessage(Message msg) {

        dialog.incrementProgressBy(increment);
        if (dialog.getProgress() == dialog.getMax()) {
            dialog.dismiss();
            TextView result = (TextView) findViewById(R.id.result);
            result.setText("heads : " + heads + "\ntails : " + tails);


        }
    }

};

}


यह टेक्स्टबॉक्स अपडेट करने के लिए संदर्भित करता है।
मपरमैन

जो मैं करना चाहता हूं वह यह है कि मेरे पास पहले से मौजूद एक बटन भी ट्रिगर हो। मैं बटन से छुटकारा नहीं चाहता क्योंकि कुछ लोग कीबोर्ड का उपयोग कर रहे होंगे जो न ही एक बटन किया हो।
मपरमैन

जवाबों:


313

आप इसे एक का उपयोग भी कर सकते हैं (एक विशेष श्रोता को सेट किया जा सकता है जब EditText पर कोई कार्रवाई की जाती है), यह DONE और RETURN दोनों के लिए काम करता है:

max.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                Log.i(TAG,"Enter pressed");
            }    
            return false;
        }
    });

1
मैं इसे क्लिक पर ट्रिगर करने के लिए कैसे सेट कर सकता हूं मैं पहले से ही सबमिट बटन को बंद कर रहा हूं सार्वजनिक शून्य पर क्लिक करें (देखें देखें) {
mpeerman

3
आप या तो सभी कोड को एकल फ़ंक्शन में ले जा सकते हैं और फिर इसे कॉल कर सकते हैं या प्रदर्शन क्लिक कर सकते हैं ()
vladexologija

6
DONE बटन दबाते समय यह मेरे लिए काम नहीं करता था, KeyEvent हमेशा शून्य था। हालांकि फिर भी कार्रवाई करने के लिए EditorInfo.IME_ACTION_DONE पर सेट किया गया था और मैं इसके बजाय इसका उपयोग करने में सक्षम था। (यह एंड्रॉइड 4.2.2 में है और इस समय एंड्रॉइड एसडीके प्रलेखन से मेल नहीं खाता है)
जेम्स

28

आप के साथ कोशिश कर सकते हैं IME_ACTION_DONE

यह क्रिया इनपुट के बिना कुछ भी करने के लिए "किया गया" ऑपरेशन करता है और IME बंद हो जाएगा।

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

8
यह एक मेरे लिए एकदम सही काम करता है। TRUE के बारे में सावधान रहें। कीबोर्ड प्रदर्शित रहता है। यदि आप कीबोर्ड रिटर्न FALSE छिपाना चाहते हैं।
मटवस्क

1
मणि की तरह काम करता है!
सैम

16

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

max.setOnKeyListener(new OnKeyListener(){
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event){
        if(keyCode == event.KEYCODE_ENTER){
            //do what you want
        }
    }
});

जब आप किसी भी बटन को धक्का देते हैं, तो आपका दृश्य onKey घटना को उत्पन्न करता है, और जब keyCode सही बटन के विरुद्ध मिलान करता है, तो यह कुछ ऐसा करता है जो आप चाहते हैं
रोमन ब्लैक

1
बटन पर ध्यान केंद्रित करना होगा
जेफरी ब्लाटमैन 18

8

Xamarin.Android के लिए यह प्रयास करें (क्रॉस प्लेटफ़ॉर्म)

edittext.EditorAction += (object sender, TextView.EditorActionEventArgs e) {
       if (e.ActionId.Equals (global::Android.Views.InputMethods.ImeAction.Done)) {
           //TODO Something
       }
};

7

कोटलिन समाधान

कोटलिन में की गई कार्रवाई को संभालने का आधार तरीका है:

edittext.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Call your code here
        true
    }
    false
}

कोटलिन एक्सटेंशन

edittext.onDone {/*action*/}अपने मुख्य कोड में कॉल करने के लिए इसका उपयोग करें । इसे अधिक पठनीय और बनाए रखने योग्य बनाता है

edittext.onDone { submitForm() }

fun EditText.onDone(callback: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            callback.invoke()
            true
        }
        false
    }
}

इन विकल्पों को अपने edittext में जोड़ना न भूलें

<EditText ...
    android:imeOptions="actionDone"
    android:inputType="text"/>

यदि आपको inputType="textMultiLine"समर्थन की आवश्यकता है , तो इस पोस्ट को पढ़ें


4

जब आप एक LoginActivity बनाते हैं तो मैंने AndroidStudio से निम्न कोड कॉपी किया। मैं ime विशेषताओं का उपयोग करता हूं

अपने लेआउट में

<EditText android:id="@+id/unidades" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:hint="@string/prompt_unidades"
                    android:inputType="number" android:maxLines="1"
                    android:singleLine="true"
                    android:textAppearance="?android:textAppearanceSmall"
                    android:enabled="true" android:focusable="true"
                    android:gravity="right"
                    android:imeActionId="@+id/cantidad"
                    android:imeActionLabel="@string/add"
                    android:imeOptions="actionUnspecified"/>

आपकी गतिविधि में

editTextUnidades.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == R.id.cantidad || actionId == EditorInfo.IME_NULL) {
                addDetalle(null);
                return true;
            }
            return false;
        }
    });

2

आप मुख्य श्रोता पर लागू कर सकते हैं:

public class ProbabilityActivity extends Activity implements OnClickListener, View.OnKeyListener {

OnCreate में:

max.setOnKeyListener(this);

...

@Override
public boolean onKey(View v, int keyCode, KeyEvent event){
    if(keyCode == event.KEYCODE_ENTER){
        //call your button method here
    }
    return true;
}

1

यदि आप अपना काम करने के लिए कीबोर्ड एंटर बटन को पकड़ना चाहते हैं, जिसे आप बटन क्लिक जैसी किसी भी घटना के माध्यम से करना चाहते हैं, तो आप उस पाठ दृश्य के लिए नीचे सरल कोड लिख सकते हैं

Edittext ed= (EditText) findViewById(R.id.edit_text);

ed.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Do you job here which you want to done through event
    }
    return false;
}
});

1

कोटलिन और न्यूमेरिक कीबोर्ड

यदि आप संख्यात्मक कीबोर्ड का उपयोग कर रहे हैं तो आपको कीबोर्ड को खारिज करना होगा, यह इस प्रकार होगा:

editText.setOnEditorActionListener { v, actionId, event ->
  if (action == EditorInfo.IME_ACTION_DONE || action == EditorInfo.IME_ACTION_NEXT || action == EditorInfo.IME_ACTION_UNSPECIFIED) {
      //hide the keyboard
      val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
      imm.hideSoftInputFromWindow(windowToken, 0)
      //Take action
      editValue.clearFocus()
      return true
  } else {
      return false
  }
}

0

अपने लेआउट में इस वर्ग का उपयोग करें:

public class ActionEditText extends EditText
{
    public ActionEditText(Context context)
    {
        super(context);
    }

    public ActionEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ActionEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs)
    {
        InputConnection conn = super.onCreateInputConnection(outAttrs);
        outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
        return conn;
    }

}

Xml में:

<com.test.custom.ActionEditText
                android:id="@+id/postED"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:gravity="top|left"
                android:hint="@string/msg_type_message_here"
                android:imeOptions="actionSend"
                android:inputType="textMultiLine"
                android:maxLines="5"
                android:padding="5dip"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                android:textColor="@color/white"
                android:textSize="20sp" />

0
max.setOnKeyListener(new OnKeyListener(){
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event){
    if(keyCode == event.KEYCODE_ENTER){
        //do what you want
    }
  }
});

1
अपने कोड पर टिप्पणी करने से हमेशा एक उत्तर की पठनीयता में सुधार होता है ...
jAC

हालांकि यह कोड प्रश्न का उत्तर दे सकता है, लेकिन समस्या का हल कैसे और / या इसके बारे में अतिरिक्त संदर्भ प्रदान करता है, इससे उत्तर के दीर्घकालिक मूल्य में सुधार होगा। याद रखें कि आप भविष्य में पाठकों के लिए सवाल का जवाब दे रहे हैं, न कि केवल उस व्यक्ति से जो अब पूछ रहा है! कृपया स्पष्टीकरण जोड़ने के लिए अपने उत्तर को संपादित करें, और इस बात का संकेत दें कि क्या सीमाएँ और मान्यताएँ लागू होती हैं। यह भी उल्लेख करने के लिए चोट नहीं करता है कि यह उत्तर दूसरों की तुलना में अधिक उपयुक्त क्यों है।
देव-इल

0

आपका अंतिम Edittext .setOnEditorActionListener इस विधि को स्वचालित हिट एपीआई कहता है

मुझे et_password में LoginActivity में कॉल किया गया था

 et_Pass.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {

                    Log.i(TAG,"Enter pressed");
                    Log.i(Check Internet," and Connect To Server");

                }
                return false;
            }
        });

अच्छा कर रहा है


0

और यह एक कोटलिन संस्करण है:

editText.setOnEditorActionListener { v, actionId, event ->
  if(actionId == EditorInfo.IME_ACTION_DONE){
      //Put your action there
      true
  } else {
      false
  }
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.