OnKey does not work with mobile keys

1

I have this code:

  nuevo.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if((event.getAction()==KeyEvent.ACTION_DOWN)&&(keyCode==KeyEvent.KEYCODE_ENTER))
            {

                String articulo;

                articulo = nuevo.getText().toString();
                listas.add(new ListasSecundarias(articulo));

                if (listas.isEmpty())
                {
                    adapter.notifyItemInserted(0);
                }
                adapter.notifyItemInserted(listas.size()+1);

                guardar(articulo,NombreDeLaLista);
                nuevo.setText("");
            }
            return false;
        }
    });

and what happens is that if I press enter with the keyboard of the PC, it performs the corresponding actions, but when I press enter on the mobile key, nothing happens.

    
asked by Sergio 27.12.2017 в 22:46
source

1 answer

-1

The onKey is only used to detect hardware events, more info here onKey

If you want to detect the enter or 'done' of your virtual keyboard, what you should do is the following

To your EditText in your xml, give the option of actionDone in imeOptions :

<EditText 
    android:id="@+id/edittext_done"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    />

Then in your code add the following:

nuevo.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
       //aquí tu lógica ...

       return true;
    }
    return false;
    } 
});

Note: Try to enable the virtual keyboard of your simulator

    
answered by 28.12.2017 в 02:03