我想要四个edittext来插入像ABC-0000-0000-0000这样的数据,因为我在一个edittext填充时应用了textwatcher向前移动,当按下delete键时它应该向后移动。为此,我应用了OnKeyListener,这是不能在android6.0.1以下工作
我的代码是 - enter image description here
editToken_1 =(EditText)alertLayout.findViewById(R.id.number1);
editToken_2 = (EditText) alertLayout.findViewById(R.id.number2);
editToken_3 =(EditText)alertLayout.findViewById(R.id.number3);
editToken_4 = (EditText) alertLayout.findViewById(R.id.number4);
editToken_1.addTextChangedListener(textWatcher);
editToken_2.addTextChangedListener(textWatcher);
editToken_3.addTextChangedListener(textWatcher);
editToken_4.addTextChangedListener(textWatcher);
editToken_1.setOnKeyListener(returnKeyListener);
editToken_2.setOnKeyListener(returnKeyListener);
editToken_3.setOnKeyListener(returnKeyListener);
editToken_4.setOnKeyListener(returnKeyListener);
EditText.OnKeyListener returnKeyListener = new EditText.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (v.getId() == editToken_4.getId() && (TextUtils.isEmpty(editToken_4.getText())) || editToken_4.getSelectionStart()==0) {
editToken_3.setSelection(editToken_3.getText().length());
editToken_3.requestFocus();
}
if (v.getId() == editToken_3.getId() && (TextUtils.isEmpty(editToken_3.getText())) || editToken_3.getSelectionStart()==0 ) {
editToken_2.setSelection(editToken_2.getText().length());
editToken_2.requestFocus();
}
if (v.getId() == editToken_2.getId() && (TextUtils.isEmpty(editToken_2.getText())) || editToken_2.getSelectionStart()==0){
editToken_1.setSelection(editToken_1.getText().length());
editToken_1.requestFocus();
}
}
return false;
}
};
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG,"beforeharSequence s "+s+"\nstart "+start+"\ncount "+count+"\nafter "+after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG," OnTextCharSequence s "+s+"\nstart "+start+"\nbefore "+before+"\ncount "+count);
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG," afterCharSequence s "+s);
if (isFilledForFirst(editToken_1) && isFilled(editToken_2) && isFilled(editToken_3) && isFilled(editToken_4)) {
closeKB();
return;
}
if (isFilledForFirst(editToken_1) && !isFilled(editToken_2)) {
editToken_2.requestFocus();
return;
}
if (isFilled(editToken_2) && !isFilled(editToken_3)) {
editToken_3.requestFocus();
return;
}
if (isFilled(editToken_3) && !isFilled(editToken_4)) {
editToken_4.requestFocus();
return;
}
}
};
void closeKB() {
if (getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
private boolean isFilled(EditText editText) {
if(editText.getText().toString().trim().length()==4 && !TextUtils.isEmpty(editText.getText()))
{
return !TextUtils.isEmpty(editText.getText());
}
return false;
}
private boolean isFilledForFirst(EditText editText) {
if(editText.getText().toString().trim().length()==3 && !TextUtils.isEmpty(editText.getText()))
{
return !TextUtils.isEmpty(editText.getText());
}
return false;
}
String getFinalCodeFromEditTexts() {
String Token = String.format(Locale.getDefault(), "%s%s%s%s%s%s%s", editToken_1.getText().toString().trim(), space_1.getText().toString().trim(), editToken_2.getText().toString().trim(), space_2.getText().toString().trim(), editToken_3.getText().toString().trim(), space_3.getText().toString().trim(), editToken_4.getText().toString().trim());
if (Token.length() == 18) {
Toast.makeText(MainActivity.this, "token is" + Token, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please Enter valid Token", Toast.LENGTH_SHORT).show();
}
return Token;
}
答案 0 :(得分:0)
发生键码事件时添加return true;
。没有它,听众就不会开火。
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (v.getId() == editToken_4.getId() && (TextUtils.isEmpty(editToken_4.getText())) || editToken_4.getSelectionStart()==0) {
editToken_3.setSelection(editToken_3.getText().length());
editToken_3.requestFocus();
}
if (v.getId() == editToken_3.getId() && (TextUtils.isEmpty(editToken_3.getText())) || editToken_3.getSelectionStart()==0 ) {
editToken_2.setSelection(editToken_2.getText().length());
editToken_2.requestFocus();
}
if (v.getId() == editToken_2.getId() && (TextUtils.isEmpty(editToken_2.getText())) || editToken_2.getSelectionStart()==0){
editToken_1.setSelection(editToken_1.getText().length());
editToken_1.requestFocus();
}
return true; //add this
}
return false;
}
};