gpt4 book ai didi

android - 如何在android中设置美国格式的电话号码?

转载 作者:行者123 更新时间:2023-11-29 14:33:36 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我想在其中设置美国格式的电话号码,例如 xxx-xxx-xxxx。我怎样才能做到这一点?

最佳答案

尝试实现下面的代码然后你会在输入数字时得到“-”

    text.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before, int count) {

boolean flag = true;
String eachBlock[] = text.getText().toString().split("-");
for (int i = 0; i < eachBlock.length; i++)
{
if (eachBlock[i].length() > 3)
{
Log.v("11111111111111111111","cc"+flag + eachBlock[i].length());
}
}
if (flag) {
text.setOnKeyListener(new OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_DEL)
keyDel = 1;
return false;
}
});

if (keyDel == 0) {

if (((text.getText().length() + 1) % 4) == 0)
{
if (text.getText().toString().split("-").length <= 2)
{
text.setText(text.getText() + "-");
text.setSelection(text.getText().length());
}
}
a = text.getText().toString();
}
else
{
a = text.getText().toString();
keyDel = 0;
}

} else {
text.setText(a);
}

}

public void beforeTextChanged(CharSequence s, int start, int count,int after)
{


}

public void afterTextChanged(Editable s) {


}


});

关于android - 如何在android中设置美国格式的电话号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9600363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com