Reputation: 240
What I want is the EditText
to accept only one input value which is a number and to have password attribute. I also want, if I enter value, say in first EditText
, I should focus to next EditText
. Similarly, if I press delete button of softkeyboard, its focus should go backwards like say, from EditText
2 to EditText
1. Mix all these and password attribute doesn't work properly.
And don't downvote without actually trying out the code.
This is the layout I have right now.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_10"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="@+id/pinc_1"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.16"
android:background="@drawable/edittext_border"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1"
android:textColor="@color/border_color" />
<EditText
android:id="@+id/pinc_2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.16"
android:background="@drawable/edittext_border"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1"
android:textColor="@color/border_color" />
<EditText
android:id="@+id/pinc_3"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.16"
android:background="@drawable/edittext_border"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1"
android:textColor="@color/border_color" />
<EditText
android:id="@+id/pinc_4"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.16"
android:background="@drawable/edittext_border"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1"
android:textColor="@color/border_color" />
<EditText
android:id="@+id/pinc_5"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.16"
android:background="@drawable/edittext_border"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1"
android:textColor="@color/border_color" />
<EditText
android:id="@+id/pinc_6"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.16"
android:background="@drawable/edittext_border"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="1"
android:textColor="@color/border_color" />
</LinearLayout>
Java Code ::
pinc_1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (pinc_1.length() == 1) {
pinc_1.clearFocus();
pinc_2.requestFocus();
pinc_2.setCursorVisible(true);
} else {
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
pinc_2.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (pinc_2.length() == 1) {
pinc_2.clearFocus();
pinc_3.requestFocus();
pinc_3.setCursorVisible(true);
} else {
pinc_2.clearFocus();
pinc_1.requestFocus();
pinc_1.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
pinc_3.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (pinc_3.length() == 1) {
pinc_3.clearFocus();
pinc_4.requestFocus();
pinc_4.setCursorVisible(true);
} else {
pinc_3.clearFocus();
pinc_2.requestFocus();
pinc_2.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
pinc_4.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (pinc_4.length() == 1) {
pinc_4.clearFocus();
pinc_5.requestFocus();
pinc_5.setCursorVisible(true);
} else {
pinc_4.clearFocus();
pinc_3.requestFocus();
pinc_3.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
pinc_5.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (pinc_5.length() == 1) {
pinc_5.clearFocus();
pinc_6.requestFocus();
pinc_6.setCursorVisible(true);
} else {
pinc_5.clearFocus();
pinc_4.requestFocus();
pinc_4.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
pinc_6.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (pinc_6.length() == 1) {
pinc_6.clearFocus();
InputMethodManager inputManager =
(InputMethodManager) getActivity().
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
getActivity().getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
} else {
pinc_6.clearFocus();
pinc_5.requestFocus();
pinc_5.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
I have also tried this way::
pinc_2.setFilters(new InputFilter[]{back_filter_pin2, new InputFilter.LengthFilter(1)});
InputFilter back_filter_pin2 = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end == 0) {
pinc_1.requestFocus();
} else if (end == 1) {
pinc_2.clearFocus();
pinc_3.requestFocus();
}
return source;
}
};
Also have tried:
pinc_6.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_VARIATION_PASSWORD);
Upvotes: 1
Views: 786
Reputation:
First of all use your condition inside
public void afterTextChanged(Editable s) {
}
To focus your cursor to previous field when delete text you can check length with 0 like this ...
if (pinc_2.length() == 1) {
pinc_2.clearFocus();
pinc_3.requestFocus();
pinc_3.setCursorVisible(true);
} else if (pinc_2.length() == 0) {
pinc_2.clearFocus();
pinc_1.requestFocus();
pinc_1.setCursorVisible(true);
}
I tested it ... it will work... Enjoy coding :)
Complete code for all EditText
pinc_1.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) {
if (s.length() == 1) {
pinc_1.clearFocus();
pinc_2.requestFocus();
pinc_2.setCursorVisible(true);
} else {
}
}
});
pinc_2.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) {
if (s.length() == 1) {
pinc_2.clearFocus();
pinc_3.requestFocus();
pinc_3.setCursorVisible(true);
} else if (s.length() == 0) {
pinc_2.clearFocus();
pinc_1.requestFocus();
pinc_1.setCursorVisible(true);
}
}
});
pinc_3.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) {
if (s.length() == 1) {
pinc_3.clearFocus();
pinc_4.requestFocus();
pinc_4.setCursorVisible(true);
} else if (s.length() == 0) {
pinc_3.clearFocus();
pinc_2.requestFocus();
pinc_2.setCursorVisible(true);
}
}
});
pinc_4.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) {
if (s.length() == 1) {
pinc_4.clearFocus();
pinc_5.requestFocus();
pinc_5.setCursorVisible(true);
} else if (s.length() == 0) {
pinc_4.clearFocus();
pinc_3.requestFocus();
pinc_3.setCursorVisible(true);
}
}
});
pinc_5.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) {
if (s.length() == 1) {
pinc_5.clearFocus();
pinc_6.requestFocus();
pinc_6.setCursorVisible(true);
} else if (s.length() == 0) {
pinc_5.clearFocus();
pinc_4.requestFocus();
pinc_4.setCursorVisible(true);
}
}
});
pinc_6.addTextChangedListener(new TextWatcher() {
CharSequence privText = "";
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
privText = s;
}
public void afterTextChanged(Editable s) {
if (s.length() > 1) {
pinc_6.setText(privText);
} else if (s.length() == 0) {
pinc_6.clearFocus();
pinc_5.requestFocus();
pinc_5.setCursorVisible(true);
}
}
});
Upvotes: 2
Reputation: 442
Change Your code as
pinc_1.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) {
// TODO Auto-generated method stub
if (pinc_1.length() == 1) {
pinc_1.clearFocus();
pinc_2.requestFocus();
pinc_2.setCursorVisible(true);
} else {
}
}
});
pinc_2.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) {
// TODO Auto-generated method stub
if (pinc_2.length() == 1) {
pinc_2.clearFocus();
pinc_3.requestFocus();
pinc_3.setCursorVisible(true);
} else {
pinc_2.clearFocus();
pinc_1.requestFocus();
pinc_1.setCursorVisible(true);
}
}
});
pinc_3.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) {
// TODO Auto-generated method stub
if (pinc_3.length() == 1) {
pinc_3.clearFocus();
pinc_4.requestFocus();
pinc_4.setCursorVisible(true);
} else {
pinc_3.clearFocus();
pinc_2.requestFocus();
pinc_2.setCursorVisible(true);
}
}
});
pinc_4.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) {
// TODO Auto-generated method stub
if (pinc_4.length() == 1) {
pinc_4.clearFocus();
pinc_5.requestFocus();
pinc_5.setCursorVisible(true);
} else {
pinc_4.clearFocus();
pinc_3.requestFocus();
pinc_3.setCursorVisible(true);
}
}
});
pinc_5.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) {
// TODO Auto-generated method stub
if (pinc_5.length() == 1) {
pinc_5.clearFocus();
pinc_6.requestFocus();
pinc_6.setCursorVisible(true);
} else {
pinc_5.clearFocus();
pinc_4.requestFocus();
pinc_4.setCursorVisible(true);
}
}
});
pinc_6.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) {
// TODO Auto-generated method stub
if (pinc_6.length() == 1) {
pinc_6.clearFocus();
InputMethodManager inputManager =
(InputMethodManager) getActivity().
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
getActivity().getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
} else {
pinc_6.clearFocus();
pinc_5.requestFocus();
pinc_5.setCursorVisible(true);
}
}
});
It is working fine. I have tested it.
Upvotes: 1
Reputation: 125
First make the editText xml input type to number password only.
android:inputType="numericPassword"
and the size of it
android:maxLength="1"
For the editText then..
editText1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(editText1.getText().length() == 1)
editText2.requestFocus();
return false;
}
});
Upvotes: 1