Diptopol Dam
Diptopol Dam

Reputation: 815

Action of edittext in android

Motivation: I want when i press enter while passing text in edittext then the work of edittext will be stopped and edittext will be invisible . My code is here

edt=(EditText) findViewById(R.id.edt);
            edt.setVisibility(View.VISIBLE);
            edt.setOnKeyListener(new View.OnKeyListener() {

                @Override
                public boolean onKey(View arg0, int arg1, KeyEvent arg2) 
                {
                    if(arg2.getAction()==KeyEvent.KEYCODE_ENTER)
                    {
                        edt.setVisibility(View.INVISIBLE);
                    }
                    return false;
                }
            });

But it is not working .What is the problem??

Upvotes: 1

Views: 967

Answers (1)

magicman
magicman

Reputation: 1199

getAction() doesn't give you the keycode. You want to use getKeyCode() instead.

change

if(arg2.getAction()==KeyEvent.KEYCODE_ENTER)

to

if(arg2.getKeyCode()==KeyEvent.KEYCODE_ENTER)

also if I understood you correctly, you want the EditText to completely disappear. If you're using a linearlayout or something similar, the EditText will be invisible, but it will still be there, taking up space. If you want the edittext to completely go away, you should use edt.setVisibilty(View.GONE) instead of edt.setVisibilty(View.INVISIBLE)

Upvotes: 2

Related Questions