Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically move to the next edit text in android

Tags:

android

I have a couple of edit text boxes on a single line. After the user types in a specific number of characters in the first I want to automatically move to the next edit text. How do I achieve this?

like image 682
user1667307 Avatar asked Sep 18 '12 03:09

user1667307


People also ask

What is Imeoption android?

android:imeOptions="actionSend" /> You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND . For example: findViewById<EditText>(R.

How do I stop deleting the first character of edit text android?

First, you can simply have it so that if the EditText block is empty, it is immediately repopulated with a "/" char. Alternatively, make it so that if the previous char is / , then prevent the user from deleting back.

How do I change the style of edit text in android Studio?

You can use the attribute style="@style/your_style" that is defined for any widget. The attribute parent="@android:style/Widget. EditText" is important because it will ensure that the style being defined extends the basic Android EditText style, thus only properties different from the default style need to be defined.


2 Answers

You can achieve this by using the Text Watcher class and then set the focus on the next EditText in the OnTextChanged() method of the TextWatcher.

In your case, since you have two Edit Texts, say et1 and et2. You can try out the following code:-

et1.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start,int before, int count) 
    {
        // TODO Auto-generated method stub
        if(et1.getText().toString().length()==size)     //size as per your requirement
        {
            et2.requestFocus();
        }
    }
    public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

    }

    public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
    }

});

I have not checked out the code myself, but I hope this will help you solve your problem.

like image 123
Shekhar Chikara Avatar answered Oct 19 '22 23:10

Shekhar Chikara


There is a simpler way to do this which doesn't involve knowledge of the ids of the EditText views. For use with android:maxLength="1".

// onTextChanged
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
    TextView text = (TextView)getCurrentFocus();

    if (text != null && text.length() > 0)
    {
        View next = text.focusSearch(View.FOCUS_RIGHT); // or FOCUS_FORWARD
        if (next != null)
            next.requestFocus();

        doSearch(); // Or whatever
    }
}

// afterTextChanged
@Override
public void afterTextChanged(Editable s) {}

// beforeTextChanged
@Override
public void beforeTextChanged(CharSequence s, int start,
                              int count, int after) {}
like image 25
Bill Farmer Avatar answered Oct 19 '22 23:10

Bill Farmer