I'm trying to detect when "Go" is pressed on the android Keyboard (the last key on the bottom right).
My code works in the emulator and on my Nexus 5, but doesn't on some other Android devices.
What am I doing wrong?

editTextMain.setImeActionLabel("Go", KeyEvent.KEYCODE_ENTER);
editTextMain.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
         if(i==KeyEvent.KEYCODE_ENTER)
         {
            Intent intent = new Intent(thisActivity, ActivityRisultati.class);
            startActivity(intent);
            return true;
         }
    }

}
有帮助吗?

解决方案

Try this

editText = (EditText) findViewById(R.id.edit_text);

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
        }
        return false;
    }
});

其他提示

I have some code in my app which detects when any key which sends the KEYCODE_ENTER value is pressed. It detects both the Enter and Done keys on a Motorola Droid 3 slide-out keyboard. Here's my code:

private OnKeyListener enterKeyListener = new OnKeyListener()
{
    public boolean onKey(View v, int k, KeyEvent e)
    {
        /*
         * "Enter" or "Done" key was pressed
         */

        if( (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (e.getAction() == KeyEvent.ACTION_UP) )
        {
            //
            // Do some stuff ...
            //

            return true;
        }

        return false;
    }
};

Note the additional check for KeyEvent.ACTION_UP. You might need that on some devices in order to prevent the listener from firing as soon as the button is pressed, and repeatedly if the button is held down. In my case, I only wanted it to take an action if the key had been pressed and then released.

Hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top