Customizing EditText and Android back button

I want to override the back button when the soft keyboard is displayed. Basically, when the back button is pressed, I want the keyboard to decline, and I want to add some text to what the user typed in this edit text box. So basically I need to know when the keyboard deviates. After searching, I realized that there is no API for this, and your EditText class will be the only real way to do this.

So, I created my own EditText class and an extended EditText like this

public class CustomEditText extends EditText
{

    public CustomEditText(Context context)
    {
        super(context);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    private void init()
    {

    }

}

I also added this method

    @Override
        public boolean dispatchKeyEventPreIme(KeyEvent event)
        {
            if (KeyEvent.KEYCODE_BACK == event.getKeyCode())
            {
                Log.v("", "Back Pressed");

                            //Want to call this method which will append text
                            //init();
            }
            return super.dispatchKeyEventPreIme(event);
        }

Now this method overrides the "Back" button, it closes the keyboard, but I do not know how to transfer text to the EditText field. Does anyone know how I will do this?

: - , ? , , , , "", , - , ?

!

+5
2

, dispatchKeyEventPreIme ACTION_DOWN, ACTION_UP.
KEY.

if(event.getAction () == KeyEvent.ACTION_DOWN)

:

setText(getText().toString() + " whatever you want to append"); 

in dispatchKeyEventPreIme

+5

? , .

+3

All Articles