
The User can enter only one digit in the edit text. if he enters the value in
You can use use a hidden EditText receive input, four TextView to display the password.
example:
dialog_passworld.xml
PasswordDialogBuilder.java
public class PasswordDialogBuilder extends AlertDialog.Builder {
public PasswordDialogBuilder(Context context) {
super(context, displayMetrics, eventManager);
View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_password, null);
LinearLayout passcodeContainer = (LinearLayout) contentView.findViewById(R.id.passcode_container);
final List passwordViews = new ArrayList();
int childCount = passcodeContainer.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = passcodeContainer.getChildAt(i);
if (childView instanceof TextView) {
passwordViews.add((TextView) childView);
}
}
final int passwordCount = passwordViews.size();
EditText passwordView = (EditText) contentView.findViewById(R.id.passcode);
passwordView.setFilters(new InputFilter[] {new InputFilter.LengthFilter(passwordCount)});
passwordView.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
passwordView.setCursorVisible(false);
passwordView.setTextColor(Color.TRANSPARENT);
passwordView.setBackgroundColor(Color.TRANSPARENT);
passwordView.setSingleLine();
passwordView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {
int passwordLength = s.length();
for (int i = 0; i < passwordCount; i++) {
TextView passcodeView = passwordViews.get(i);
if (i < passwordLength) {
passcodeView.setText(String.valueOf(s.charAt(i)));
} else {
passcodeView.setText(StringUtils.EMPTY);
}
}
if (positiveButton != null) {
positiveButton.setEnabled(passwordLength == passwordCount);
}
}
});
setView(contentView);
}
}