Frage

I have created a library project for creating a pass code Layout. I have created four text boxes for entering pass code, each text box can contain only one character. But I am unable to create an event for returning the pass code. I want to fire an event when user enter 4 digit pass code.

Here is xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/ll_passcodedigits"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >

    <EditText
        android:id="@+id/et_passcode"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="textPassword"
        android:maxLength="1" />

    <EditText
        android:id="@+id/et_passcode2"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="textPassword"
        android:maxLength="1" />

    <EditText
        android:id="@+id/et_passcode3"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="textPassword"
        android:maxLength="1" />

    <EditText
        android:id="@+id/et_passcode4"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="textPassword"
        android:maxLength="1" />
</LinearLayout>

</LinearLayout>

And Here is the code of class:

public class Passcode extends LinearLayout {

EditText firstEditText, secondEditText, thirdEditText, fourEditText;

public Passcode(Context context) {
    super(context);
    // TODO Auto-generated constructor stub

}

public Passcode(Context context, AttributeSet attrs) {
    super(context, attrs);

    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.passcode_layout, this);

    loadViews();
}

private void loadViews() {
    firstEditText = (EditText) findViewById(R.id.et_passcode);
    secondEditText = (EditText) findViewById(R.id.et_passcode2);
    thirdEditText = (EditText) findViewById(R.id.et_passcode3);
    fourEditText = (EditText) findViewById(R.id.et_passcode4);

    // Text changed event for first EditText
    firstEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (s.length() == 1) {
                firstEditText.setFocusable(false);
                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

        }
    });

    // Text changed event for second EditText
    secondEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (s.length() == 1) {
                secondEditText.setFocusable(false);
                thirdEditText.requestFocus();
            } else if (s.length() == 0) {
                firstEditText.setFocusable(true);
                firstEditText.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

        }
    });

    // Text changed event for third EditText
    thirdEditText.addTextChangedListener(new TextWatcher() {

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

            if (s.length() == 1) {
                thirdEditText.setFocusable(false);
                fourEditText.requestFocus();
            } else if (s.length() == 0) {
                secondEditText.setFocusable(true);
                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

        }
    });

    // Text changed event for fourth EditText
    fourEditText.addTextChangedListener(new TextWatcher() {

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

            if (s.length() == 1) {
                thirdEditText.setFocusable(false);

            } else if (s.length() == 0) {
                thirdEditText.setFocusable(true);
                thirdEditText.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

        }
    });
}



  }
War es hilfreich?

Lösung

You can define your own listener.

Add this to Passcode:

public interface OnPasscodeCompletedListener {
  public void onComplete(String passcode);
}

private OnPasscodeCompletedListener listener = null;

public void setOnPasscodeCompletedListener(OnPasscodeCompletedListener listener) {
  this.listener = listener;
}

Then, in all your text changed listeners, add this when you have all 4 digits:

if (listener != null) {
  // Concatenate the values from EditText to create the passcode
  listener.onComplete(passcode);
}

After that, use it like how you'd use a onClickListener. For example:

private OnPasscodeCompletedListener onPasscodeCompletedListener
    = new OnPasscodeCompletedListener() {
  @Override
  public void onComplete(String passcode) {
    Log.i(TAG, "passcode = " + passcode);
  }
};
passcodeView.setOnPasscodeCompletedListener(onPasscodeCompletedListener);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top