1

I have an option in App to enter 4 digit PIN number in android For that I have taken One Linear Layout and four Edittext

Like this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <!--<ImageButton-->
    <!--android:id="@+id/imageButton3"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:background="#25313f"-->
    <!--android:padding="@dimen/dimen_10dp"-->
    <!--android:contentDescription="@string/app_name"-->
    <!--android:src="@drawable/ic_pwdlogin" />-->


    <EditText
        android:id="@+id/EdtPin1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="1dp"
        android:layout_weight="0.25"
        android:background="#fff"
        android:inputType="numberPassword"
        android:maxLength="1"
        android:paddingLeft="24dp"
        android:singleLine="true"
        android:textColor="#111"
        android:textColorHint="#FFFFFF" />

    <EditText
        android:id="@+id/EdtPin2"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="1dp"
        android:layout_weight="0.25"
        android:background="#fff"
        android:inputType="numberPassword"
        android:maxLength="1"
        android:paddingLeft="24dp"
        android:singleLine="true"
        android:textColor="#111"
        android:textColorHint="#FFFFFF" />

    <EditText
        android:id="@+id/EdtPin3"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="1dp"
        android:layout_weight="0.25"
        android:background="#fff"
        android:inputType="numberPassword"
        android:maxLength="1"
        android:paddingLeft="24dp"
        android:singleLine="true"
        android:textColor="#111"
        android:textColorHint="#FFFFFF" />


    <EditText
        android:id="@+id/EdtPin4"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="1dp"
        android:layout_weight="0.25"
        android:background="#fff"
        android:inputType="numberPassword"
        android:maxLength="1"
        android:paddingLeft="24dp"
        android:singleLine="true"
        android:textColor="#111"
        android:textColorHint="#FFFFFF" />


</LinearLayout>

But Problem is that Suppose I press backspace in 4th edittext then first time press backspace at that time it should be remain in same edittext but when i press backspace again it should be go to its previous backspace. But when Edittext is blank i could not capture backpress event I have also done this code

final EditText EdtPin3 = (EditText) findViewById(R.id.EdtPin3);
EdtPin3.addTextChangedListener(new TextWatcher() {

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

    }

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

    }

    public void afterTextChanged(Editable s) {
        EditText EdtPin4 = (EditText) findViewById(R.id.EdtPin4);
        if (EdtPin4.getText().toString().equals("")) {
            EdtPin4.requestFocus();
        }

        if (EdtPin3.getText().toString().equals("")) {
            EdtPin3.requestFocus();
        }
    }
});
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Dhruvin shah
  • 197
  • 1
  • 12

1 Answers1

1
StringBuilder sb=new StringBuilder();

     edtPasscode1.addTextChangedListener(new TextWatcher() {
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             // TODO Auto-generated method stub
             if(sb.length()==0&edtPasscode1.length()==1)
             {
                 sb.append(s);
                 edtPasscode1.clearFocus();
                 edtPasscode2.requestFocus();
                 edtPasscode2.setCursorVisible(true);

             }
         }

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

             if(sb.length()==1)
             {

                 sb.deleteCharAt(0);

             }

         }

         public void afterTextChanged(Editable s) {
             if(sb.length()==0)
             {

                 edtPasscode1.requestFocus();
             }

         }
     });

Exactly what your are looking for here: How to change the focus to next edit text in android?

Community
  • 1
  • 1
johnrao07
  • 6,690
  • 4
  • 32
  • 55