1

I have three adjacent EditText fields, each of them to be filled with 6 characters of user choice. The thing that I want is that as soon as the user is done typing the 6th character in the first EditText field, the cursor automatically shifts to the second EditText field. Same for second -> third. How can I go about doing this?

scibor
  • 983
  • 4
  • 12
  • 21

4 Answers4

1

Just implement a KeyListener to your first EditText (let's say editTextOne).
At each key pressed, check if the length of editTextOne is equals to 6, and move the cursor to the second one by calling the method requestFocus() on your second editText (i.e editTextTwo.requestFocus()).

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • Also a follow up question, after the final 6 character entry has been entered, how do I force the keyboard to close on the screen? I think that's closely related? – scibor Jun 10 '13 at 13:25
  • @scibor Read this : http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – Alexis C. Jun 10 '13 at 13:29
1

use like that

edittext1.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(edittext1.getText().toString().length()==6) {
            edittext2.requestFocus();
        }
        return false;
    }
});
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
0

Try this.

mEtText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if(mEtText.getText().length() == 6){
                secondEditText.requestFocus();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

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

        }
    });
0

It is necessary in this case, use the appropriate listener: TextWatcher

Interacting with the code below:

package com.example.stackoverflow;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;

public class SimpleActivity10 extends Activity {

    private final short MAXCHAR = 6;

    private EditText editText1, editText2, editText3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.simple_activity10);
        editText1 = (EditText) this.findViewById(R.id.editText1);
        editText2 = (EditText) this.findViewById(R.id.editText2);
        editText3 = (EditText) this.findViewById(R.id.editText3);

        editText1.addTextChangedListener(textWatcher1);
        editText2.addTextChangedListener(textWatcher2);

        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(MAXCHAR);
        editText1.setFilters(FilterArray);
        editText2.setFilters(FilterArray);
        editText3.setFilters(FilterArray);

    }

    private TextWatcher textWatcher1 = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable editable) {
            int messageLength = editable.toString().length();
            if (messageLength == MAXCHAR) {
                Toast.makeText(getApplicationContext(), "EditText 2 get focus!", Toast.LENGTH_SHORT).show();
                editText2.requestFocus();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

    };

    private TextWatcher textWatcher2 = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable editable) {
            int messageLength = editable.toString().length();
            if (messageLength == MAXCHAR) {
                Toast.makeText(getApplicationContext(), "EditText 3 get focus!", Toast.LENGTH_SHORT).show();
                editText3.requestFocus();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.simple_activity10, menu);
        return true;
    }

}

And the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SimpleActivity10" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" />

</LinearLayout>
Jarvis
  • 1,527
  • 12
  • 17