android a caché le clavier Enter (dans un texte édité ))

quand mon utilisateur appuie sur Entrée sur l'android virtuel "user validate entry!"clavier Mon clavier reste visible! (Pourquoi?)

Voici mon code Java...

private void initTextField() {
    entryUser = (EditText) findViewById(R.id.studentEntrySalary);
    entryUser.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        userValidateEntry();
                        return true;
                }
            }

          return true;
        }
    });
}

private void userValidateEntry() {
    System.out.println("user validate entry!");
}

... ici mon point de Vue

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 </LinearLayout>
<!-Peut-être que quelque chose ne va pas sur mon appareil virtuel?

27
demandé sur Cœur 2010-03-12 20:24:14

5 réponses

Cela devrait le faire:

yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                // NOTE: In the author's example, he uses an identifier
                // called searchBar. If setting this code on your EditText
                // then use v.getWindowToken() as a reference to your 
                // EditText is passed into this callback as a TextView

                in.hideSoftInputFromWindow(searchBar
                        .getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
               userValidateEntry();
               // Must return true here to consume event
               return true;

            }
            return false;
        }
    });
68
répondu jqpubliq 2012-06-23 22:20:29

Conserver le singleLine="true" et d'ajouter imeOptions="actionDone" à l'EditText. Puis, dans le OnEditorActionListener, vérifiez si actionId == EditorInfo.IME_ACTION_DONE, comme (mais le changer à votre mise en œuvre):

if (actionId == EditorInfo.IME_ACTION_DONE) {

                if ((username.getText().toString().length() > 0)
                        && (password.getText().toString().length() > 0)) {
                    // Perform action on key press
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(username.getWindowToken(),
                            0);
                    doLogin();
                }
            }
18
répondu Micha Okkerman 2012-01-11 07:56:30

si vous faites de la zone de texte une seule ligne (je crois que la propriété S'appelle SingleLine dans les fichiers XML de mise en page), elle sortira du clavier enter.

voilà: http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine

5
répondu Hans 2010-03-12 17:34:02

je crée un composant personnalisé qui étend AutoCompleteTextView, comme dans l'exemple ci-dessous:

public class PortugueseCompleteTextView extends AutoCompleteTextView {
...
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event != null &&  (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
        InputMethodManager inputManager =
                (InputMethodManager) getContext().
                        getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(
                this.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }
    return super.onKeyPreIme(keyCode, event);
}

j'utilise ce code dans L'AlertDialog.Construction, mais est possible d'être utilisé à L'activité.

1
répondu Ricardo Ruas 2016-10-05 14:33:27

il suffit d'ajouter cette ligne dans votre texte à modifier.

android:imeOptions="actionDone"'

Vous pouvez spécifier le prochain ID d'édition de texte pour passer à ce texte d'édition sur le clic du bouton clavier fait.

0
répondu Satyajit Das 2018-06-19 15:15:51