0

I have four EditText's to read a PIN. Now that I have coded the Java side of the EditText it behaves in an abnormal way. After entering one digit in the first EditText it should request focus of the second EditText, however it goes to the third EditText.

enter image description here

And even more it is defined as numberPassword and still the digits are visible.

Here is the source code to the layout file :

 <RelativeLayout
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<EditText
    android:id="@+id/mpin_1"
    android:layout_marginStart="20dp"
    android:layout_centerVertical="true"
    android:maxLength="1"
    android:gravity="center"
    android:layout_width="50dp"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

<EditText
    android:id="@+id/mpin_2"
    android:layout_marginStart="20dp"
    android:layout_toEndOf="@+id/mpin_1"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:maxLength="1"
    android:layout_width="50dp"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

<EditText
    android:id="@+id/mpin_3"
    android:layout_marginStart="20dp"
    android:layout_toEndOf="@+id/mpin_2"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:layout_width="50dp"
    android:maxLength="1"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

<EditText
    android:id="@+id/mpin_4"
    android:layout_marginStart="20dp"
    android:layout_toEndOf="@+id/mpin_3"
    android:gravity="center"
    android:layout_centerVertical="true"
    android:layout_width="50dp"
    android:maxLength="1"
    android:inputType="numberPassword"
    android:layout_height="50dp" />

</RelativeLayout>

The source code to the java file:

    public class SpinActivity extends AppCompatActivity
{
    private StringBuilder s_pin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spin);
        s_pin = new StringBuilder();
        s_pin.setLength(0);

        Button sPinButton = (Button)findViewById(R.id.set_s_pin_button);

        final EditText e1 = (EditText)findViewById(R.id.mpin_1);
        final EditText e2 = (EditText)findViewById(R.id.mpin_2);
        final EditText e3 = (EditText)findViewById(R.id.mpin_3);
        final EditText e4 = (EditText)findViewById(R.id.mpin_4);

        e1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e1.getText().length()==1)
                    s_pin.append(e1.getText().toString());
                //e1.setEnabled(false);
                e2.requestFocus();
                return false;
            }
        });

        e2.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e2.getText().length()==1)
                    s_pin.append(e2.getText().toString());
                //e2.setEnabled(false);
                e3.requestFocus();
                return false;
            }
        });

        e3.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e3.getText().length()==1)
                    s_pin.append(e3.getText().toString());
                //e3.setEnabled(false);
                e4.requestFocus();
                return false;
            }
        });

        e4.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(e4.getText().length()==1)
                    s_pin.append(e4.getText().toString());
               // e4.setEnabled(false);
                return false;
            }
        });

        sPinButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(),s_pin.toString(),
                        Toast.LENGTH_SHORT).show();

            }
        });



    }


}

How to fix this?

OBX
  • 6,044
  • 7
  • 33
  • 77
  • 1
    [http://stackoverflow.com/questions/17989733/move-to-another-edittext-when-soft-keyboard-next-is-clicked-on-android](http://stackoverflow.com/questions/17989733/move-to-another-edittext-when-soft-keyboard-next-is-clicked-on-android) – M D Jun 02 '16 at 05:43

1 Answers1

1

Use below code:

android:nextFocusDown="@+id/edittext"

edittext is the ID of the EditText to be focused.

Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33