I have to implement an application in which I have set small edittext
for input PIN number and Mobile Number. Each edit text contain 1 character at a time.
Now when I run this app, I found that for each box I need to give focus to each edittext
.
So in this case is it possible to set focus in next edittext after character is entered in first and then goto third after character is entered in second.
so please help me out this
Here is layout image.
1. implements TextWatcher on EditTexts
and then when you get Edittext's text length is 1.
2. Change focus on other EditText using
Call requestFocus()
on the other EditText..
CODE: (This code is for your understanding, It may be different on your case and your need)
public class LoginActivity extends Activity implements TextWatcher
{
.
.
.
editTextUName = (EditText) findViewById(R.id.edttextusername);
editTextUName.addTextChangedListener(this);
editTextPwd = (EditText) findViewById(R.id.edttextpassword);
editTextPwd.addTextChangedListener(this);
@Override
public void afterTextChanged(Editable s)
{
if (s.toString().length() == 1)
{
editTextPwd.requestFocus();
}
}
Use this Code
final EditText EditText01 = (EditText) findViewById(R.id.editText01);
final EditText EditText02= (EditText) findViewById(R.id.editText02);
final EditText EditText03 = (EditText) findViewById(R.id.editText03);
final EditText EditText04 = (EditText) findViewById(R.id.editText04);
EditText01.requestFocus();
EditText01.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(final View v,
final boolean hasFocus) {
if (hasFocus) {
final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is
// open
mgr.showSoftInput(EditText01,
InputMethodManager.SHOW_IMPLICIT);
}
}
});
EditText01.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(final View v, final int keyCode,
final KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
EditText01.requestFocus();
} else {
EditText02.requestFocus();
}
}
return false;
}
});
EditText02.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(final View v, final int keyCode,
final KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
EditText01.requestFocus();
} else {
EditText03.requestFocus();
}
}
return false;
}
});
EditText03.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(final View v, final int keyCode,
final KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
EditText02.requestFocus();
} else {
EditText04.requestFocus();
}
}
return false;
}
});
EditText04.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(final View v, final int keyCode,
final KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
EditText03.requestFocus();
}
}
return false;
}
});
Add a text change listener
textView1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
textView2.requestFocus();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
This should work fine.
mPinFirst and so on are the EditTexts representing each Pin entry.
Should also take care of backspacing.
mPinFirst.setOnKeyListener(onKeyListener);
mPinSecond.setOnKeyListener(onKeyListener);
mPinThird.setOnKeyListener(onKeyListener);
mPinFourth.setOnKeyListener(onKeyListener);
View.OnKeyListener onKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(KeyEvent.KEYCODE_DEL != keyEvent.getKeyCode()) {
if (((ESEditText) view).getText().length() == 1)
if (view.focusSearch(View.FOCUS_RIGHT) != null) {
view.focusSearch(View.FOCUS_RIGHT).requestFocus();
return true;
}
}else{
((ESEditText) view).setText("");
if (view.focusSearch(View.FOCUS_LEFT) != null) {
view.focusSearch(View.FOCUS_LEFT).requestFocus();
return true;
}
}
return false;
}
};