views:

420

answers:

2

I want to cause the focus of one edit text box to move to another on editting (meaning you can only type on letter before it automatically moves on to the next edit text).

It's the "on edit" that I can't get my head around. Can anyone help me out with a simple example? Theres a lot I need to implement it into, so just a basic understanding should set the ball rolling ^_^

+1  A: 

I do not really recommend this. With soft keyboards and multiple languages, what exactly is "one letter"? After all, a soft keyboard might enter in an entire word, like it or not.

CommonsWare
A: 

CommonsWare makes an excellent point: you can't prevent the user from adding more characters to the EditText box, however you can listen to what's changed and act on that. Here's how to:

EditText editbox = (EditText) findViewById(R.id.MyEditBoxName);

editbox.addTextChangedListener(new TextWatcher()
{
    public void  beforeTextChanged(CharSequence s, int start, int count, int after)
    {

    }

    public void  onTextChanged(CharSequence s, int start, int before, int count)
    {

    }

    public void  afterTextChanged(Editable s)
    {
         // Test s for length, request focus for the next edit.
         // editbox2.requestFocus();
    }
});

Be careful not to get yourself into an infinite loop changing the editbox, any changes you make will cause these methods to be called again recursively.

Patrick de Kleijn