PhoneUtils编辑文本的等效代码

时间:2014-07-20 22:32:59

标签: android

您是否有一段代码可以手动格式化Android中的指定电话号码?我不想使用PhoneUtils。我需要这个用于我课程的项目。

1 个答案:

答案 0 :(得分: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));*/

        }