我想将这种 (888) 777-7777 格式用于电话号码。我的数字格式为“888-777-7777”。我把我的代码放在editTextChangedListener中。如何显示所需的格式?
edt_clientphone.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
boolean flag = true;
String eachBlock[] = edt_clientphone.getText().toString().split("-");
for (int i = 0; i < eachBlock.length; i++)
{
if (eachBlock[i].length() > 3)
{
Log.v("data","cc"+flag + eachBlock[i].length());
}
}
if(flag){
edt_clientphone.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_DEL)
keyDel = 1;
return false;
}
});
if (keyDel == 0) {
if (((edt_clientphone.getText().length() + 1) % 4) == 0)
{
if (edt_clientphone.getText().toString().split(")").length <= 2)
{
edt_clientphone.setText(edt_clientphone.getText() + "-");
edt_clientphone.setSelection(edt_clientphone.getText().length());
}
}
ab = edt_clientphone.getText().toString();
}
else
{
ab = edt_clientphone.getText().toString();
keyDel = 0;
}
} else {
edt_clientphone.setText(ab);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});