我必须实现一个应用程序,其中我为输入PIN码和手机号码设置了小edittext
。每个编辑文本一次包含1个字符。
现在,当我运行此应用程序时,我发现对于每个框,我需要将焦点放在每个edittext
上。
因此,在这种情况下,可以在第一个输入字符后在下一个edittext中设置焦点,然后在第二个输入字符后转到第三个。
所以请帮我解决这个问题
这是布局图片。
答案 0 :(得分:4)
1. implements TextWatcher on EditTexts
然后当你得到Edittext的文字长度是1 。
2. Change focus on other EditText using
在另一个EditText上调用requestFocus()
..
代码:(此代码仅供您理解,可能因您的情况而有所不同)
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();
}
}
答案 1 :(得分:0)
使用此代码
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;
}
});
答案 2 :(得分:0)
添加文本更改侦听器
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) {
}
});
答案 3 :(得分:0)
这应该可以正常工作。 mPinFirst等是表示每个Pin条目的EditTexts。 还应该照顾退步。
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;
}
};