The User can enter only one digit in the edit text. if he enters the value in
I also had the same problem and I just solved it, simple. With .xml file. Use android:nextFocusDown="next_EditText_id"
Ex:
<EditText
android:id="@+id/edtUsername"
android:nextFocusDown="@id/edtPassword"
style="@style/EditText"
/>
<EditText
android:id="@+id/edtPassword"
style="@style/EditText.Password"
/>
First of all Set EditText max length=1
here i take three EditText
in xml file.
<EditText
android:id="@+id/edt1"
android:layout_width="40dp"
android:layout_height="40dp"
android:inputType="number"
android:textAlignment="center"
android:singleLine="true"
android:maxLength="1"
android:maxLines="1"
android:imeOptions="actionNext"
android:layout_margin="5dp"
android:background="@drawable/edittext_shape_bg"/>
<EditText
android:id="@+id/edt2"
android:layout_width="40dp"
android:layout_height="40dp"
android:inputType="number"
android:textAlignment="center"
android:singleLine="true"
android:maxLength="1"
android:imeOptions="actionNext"
android:maxLines="1"
android:layout_margin="5dp"
android:background="@drawable/edittext_shape_bg"/>
<EditText
android:id="@+id/edt3"
android:layout_width="40dp"
android:layout_height="40dp"
android:inputType="number"
android:textAlignment="center"
android:singleLine="true"
android:maxLength="1"
android:imeOptions="actionNext"
android:maxLines="1"
android:layout_margin="5dp"
android:background="@drawable/edittext_shape_bg"/>
Then after implement addTextChangeListener
in each EditText
Which Helps You to Move Forward and Backward.
Here we use StringBuilder
to get Final String value.
edt1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String edtChar=edt1.getText().toString();
if(edtChar.length()==1)
{
currentCode.append(edtChar);
edt2.requestFocus();
}else if(edtChar.length()==0) {
currentCode.deleteCharAt(0);
edt1.requestFocus();
}
}
});
edt2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String edtChar=edt2.getText().toString();
if(edtChar.length()==1)
{
currentCode.append(edtChar);
edt3.requestFocus();
}else if(edtChar.length()==0) {
currentCode.deleteCharAt(1);
edt1.requestFocus();
}
}
});
edt3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String edtChar=edt3.getText().toString();
if(edtChar.length()==1)
{
currentCode.append(edtChar);
edt4.requestFocus();
}else if(edtChar.length()==0) {
currentCode.deleteCharAt(2);
edt2.requestFocus();
}
}
});
You can use use a hidden EditText receive input, four TextView to display the password.
example:
dialog_passworld.xml
<?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"
android:paddingBottom="10dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="10dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Please enter a password"
android:textSize="16sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginTop="15dip" >
<EditText
android:id="@+id/passcode"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/passcode_container"
android:layout_width="match_parent"
android:layout_height="40dip"
android:background="@drawable/password_border" >
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
<TextView
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
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<TextView> passwordViews = new ArrayList<TextView>();
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);
}
}