EditText & Enter key

user1114026 picture user1114026 · Jan 12, 2013 · Viewed 10k times · Source

I want the functionallity

  1. just Enter -- EditText return and submit the text
  2. shift+Enter -- new line in EditText

this is the code but it don,t work. No difference between Enter and shift+Enter (no new line):

EditText text=(EditText)findViewById(R.id.text);

text.setOnEditorActionListener( new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if ( (actionId == EditorInfo.IME_ACTION_DONE)  ||
                (  (event.isShiftPressed()==false) && 
                (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) &&
                (event.getAction() == KeyEvent.ACTION_DOWN ) ) ){

                Editable buff=(Editable)v.getText();
                writeText( buff.toString() );

                context.finish();  // texten sparad här o activity avslutas

                return true;
            }
            return false;
            }
    }); 

in layout.xml:

android:inputType="text|textMultiLine"
android:imeOptions="actionDone"

Answer

Codeman picture Codeman · Jan 12, 2013

I think you need to use a shift key listener and keep a boolean to detect when your shift key is pressed, like so:

Android shift key listener

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) 
{
    switch (v.getId()) 
    {
        case R.id.myEditTextId:
        if(keyCode==59)//59 is shift's keycode
        //do your stuff here against pressing shift key
        break;
    }
}