Question

Quand ma presse utilisateur Entrée sur l'androïde virtuel "entrée validate utilisateur!" 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 Voir mon

 <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 quelque chose de mal sur mon appareil virtuel?

Était-ce utile?

La solution

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

Autres conseils

Gardez la SingleLine = "true" et ajoutez imeOptions = "actionDone" au EditText. Ensuite, dans le OnEditorActionListener vérifier si actionID == EditorInfo.IME_ACTION_DONE, comme si (mais le changement à 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();
                }
            }

Si vous faites la zone de texte d'une seule ligne (je crois que le propery est appelé SingleLine dans la mise en page des fichiers xml), il sortira sur le clavier entrer.

vous allez ici: http://developer.android.com/ référence / android / R.styleable.html # TextView_singleLine

Je suis créer 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 le AlertDialog.Builder, mais est possible d'être en utilisant à l'activité.

il suffit d'ajouter cette ligne dans votre texte d'édition.

android:imeOptions="actionDone"'

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top