問題描述
用戶只能在編輯文本中輸入一位數字.如果他在 edtText1 中輸入值,我希望光標自動移動到 edtText2 等等.用戶可以編輯他/她已經輸入的文本.我嘗試了以下方式.
The User can enter only one digit in the edit text. if he enters the value in edtText1, I want the cursor automatically moves to edtText2 and so on. The user can able to edit the text which he/she has entered already. I tried the following way.
edtPasscode1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (edtPasscode1.getText().length() == 1)
edtPasscode2.requestFocus();
return false;
}
});
edtPasscode2.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (edtPasscode2.getText().length() == 1)
edtPasscode3.requestFocus();
return false;
}
});
edtPasscode3.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (edtPasscode3.getText().length() == 1)
edtPasscode4.requestFocus();
return false;
}
});
如果用戶編輯文本,光標會移動到其他一些editTexts并且無法按預期工作.我怎樣才能實現上述目標?
If the user edit the text, The cursor moves to some other editTexts and not working as desired. How can i achieve the above?
推薦答案
試試 TextWatcher 代替 onKeyListener
如果你想修改你的密碼,那么TextWatcher會給你更多的處理方法..
B'coz if want to edit your password, in that case TextWatcher will give you more method to dealt with..
已-
StringBuilder sb=new StringBuilder();
edtPasscode1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(sb.length()==0&edtPasscode1.length()==1)
{
sb.append(s);
edtPasscode1.clearFocus();
edtPasscode2.requestFocus();
edtPasscode2.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(sb.length()==1)
{
sb.deleteCharAt(0);
}
}
public void afterTextChanged(Editable s) {
if(sb.length()==0)
{
edtPasscode1.requestFocus();
}
}
});
希望這項工作.
這篇關于如何將焦點更改為android中的下一個編輯文本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!