我正在尝试同时使用
editPhoneNumber.setKeyListener(new KeyListener()
和
editPhoneNumber.addTextChangedListener(new TextWatcher()
在一起,但似乎它们相互阻挡,我的editText不再可编辑。
感谢您的帮助
编辑:
我正在尝试将输入格式化为用户输入:
输入:5551112233 格式化输入:(555)111 22 33
格式化部分运行良好,但我尝试添加KeyListener的原因是当用户尝试删除(通过按下删除键)并且当输入长度变为-let的时候 - 12个字符TextChangedListener中断用户删除总是在文本的末尾添加一个“”。这就是为什么我在输入是删除键时尝试禁用TextChangedListener。
boolean isResetClicked;
EditText editPhoneNumber;
Button buttonReset;
protected void onCreate(Bundle savedInstanceState)
{
editPhoneNumber = (EditText) findViewById(R.id.editPhoneNumber);
buttonReset = (Button) findViewById(R.id.buttonResetPhoneNumber);
buttonReset.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
isResetClicked = true;
editPhoneNumber.getText().clear();
isResetClicked = false;
}
});
editPhoneNumber.setKeyListener(new KeyListener()
{
@Override
public boolean onKeyUp(View view, Editable text, int keyCode,
KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DEL)
{
isResetClicked = true;
}
else
isResetClicked=false;
return false;
}
@Override
public boolean onKeyOther(View view, Editable text, KeyEvent event)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onKeyDown(View view, Editable text, int keyCode,
KeyEvent event)
{
return false;
}
@Override
public int getInputType()
{
// TODO Auto-generated method stub
return 0;
}
@Override
public void clearMetaKeyState(View view, Editable content,
int states)
{
// TODO Auto-generated method stub
}
});
editPhoneNumber.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
String str;
if (!isResetClicked)
{
if (s.length() == 0)
{
editPhoneNumber.setText("(");
}
if (s.length() == 1)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText("(" + str);
editPhoneNumber.setSelection(editPhoneNumber.getText()
.length());
}
if (s.length() == 4)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText(str + ") ");
editPhoneNumber.setSelection(editPhoneNumber.getText()
.length());
}
if (s.length() == 9)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText(str + " ");
editPhoneNumber.setSelection(editPhoneNumber.getText()
.length());
}
if (s.length() == 12)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText(str + " ");
editPhoneNumber.setSelection(editPhoneNumber.getText()
.length());
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s)
{
}
});
编辑2:
即使我仍然无法理解为什么两个事件监听器会阻止我的编辑文本,我通过修改代码解决了我的问题:
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
String str;
/*Log.i("ED",
"LengthBefore before (lengthBefore = lengthAfter;): "
+ String.valueOf(lengthBefore));*/
lengthBefore = lengthAfter;
lengthAfter = s.length();
/*Log.i("ED",
"LengthBefore after (lengthBefore = lengthAfter;): "
+ String.valueOf(lengthBefore));*/
if ((lengthBefore < lengthAfter) || lengthBefore == 0)
{
if (!isResetClicked)
{
if (s.length() == 0)
{
editPhoneNumber.setText("(");
}
if (s.length() == 1)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText("(" + str);
editPhoneNumber.setSelection(editPhoneNumber
.getText().length());
}
if (s.length() == 4)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText(str + ") ");
editPhoneNumber.setSelection(editPhoneNumber
.getText().length());
}
if (s.length() == 9)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText(str + " ");
editPhoneNumber.setSelection(editPhoneNumber
.getText().length());
}
if (s.length() == 12)
{
str = editPhoneNumber.getText().toString();
editPhoneNumber.setText(str + " ");
editPhoneNumber.setSelection(editPhoneNumber
.getText().length());
}
}
}
lengthAfter = s.length();
/*Log.i("ED", "LengthAfter after (lengthAfter = s.length();): "
+ String.valueOf(lengthAfter));
Log.i("ED", "LengthBefore: " + String.valueOf(lengthBefore));
Log.i("ED", "LengthAfter: " + String.valueOf(lengthAfter));*/
}
当用户尝试删除字符时,新长度将比旧字符短。所以我阻止代码通过以下方式检查长度:
if ((lengthBefore < lengthAfter) || lengthBefore == 0)