安卓中EditText中的电话号码格式化

3
如何在Android的edittext中输入电话号码时以(xxx)xxx-xxxxx格式设置电话号码?
我想要在特定的位置自动添加(,),-字符。
我编写了代码,但只能在Android 2.2版本中使用,不适用于以上版本。我在stackoverflow上搜寻了很多问题,但没有得到答案。
请检查我的代码:
phone.addTextChangedListener(new TextWatcher () {

            @Override
            public void afterTextChanged(Editable chars) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(final CharSequence s, final int start, int lengthBefore,
                    int lengthAfter) {
                // TODO Auto-generated method stub
                count = start;

                /*if (phone.getText().length() <= 0) {
                    phone.append("(");
                    phone.setSelection(1);
                }else if (count == 3) {
                    phone.append(")");
                }else if (count == 7) {
                    phone.append("-");
                }

            */

            }
        });



phone.setOnKeyListener(new OnKeyListener (){

                @Override
                public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                    // TODO Auto-generated method stub
                    if(arg1 == KeyEvent.KEYCODE_DEL){
                        if(count > 0){
                            count = count - 1;
                        }
                    }else{
                        if(phone.getText().length() <= 0){
                            phone.append("(");
                        }else{
                            if(count  == 3){
                                phone.append(")");
                            }else if(count == 7){
                                phone.append("-");
                            }
                        }
                    }
                    return false;
                }

            });


        }

Logcat显示如下:

01-03 15:07:23.529: W/InputManagerService(153): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40900f88
01-03 15:07:23.529: D/StatusBarPolicy(221): mFullChargeListener
01-03 15:07:25.749: V/AudioPolicyManager(84): startOutput() output 1, stream 1, session 1488
01-03 15:07:25.749: V/AudioPolicyManager(84): changeRefCount() stream 1, count 1
01-03 15:07:25.749: V/AudioPolicyManager(84): getDeviceForStrategy() from cache strategy 0, device 2
01-03 15:07:25.749: V/AudioPolicyManager(84): getNewDevice() selected device 2
01-03 15:07:25.749: V/AudioPolicyManager(84): setOutputDevice() output 1 device 2 delayMs 0
01-03 15:07:25.749: V/AudioPolicyManager(84): setOutputDevice() setting same device 2 or null device for output 1
01-03 15:07:25.749: V/AudioPolicyManager(84): releaseOutput() 1
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): open driver
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): get config
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): set config
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): buffer_size: 4800
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): buffer_count: 2
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): channel_count: 2
01-03 15:07:25.769: V/AudioHardwareMSM72XX(84): sample_rate: 44100
01-03 15:07:25.939: W/AudioFlinger(84): write blocked for 164 msecs, 61 delayed writes, thread 0xcdd0
01-03 15:07:26.009: V/AudioPolicyManager(84): stopOutput() output 1, stream 1, session 1488
01-03 15:07:26.009: V/AudioPolicyManager(84): changeRefCount() stream 1, count 0
01-03 15:07:26.009: V/AudioPolicyManager(84): getNewDevice() selected device 0
01-03 15:07:26.009: V/AudioPolicyManager(84): setOutputDevice() output 1 device 0 delayMs 0
01-03 15:07:26.009: V/AudioPolicyManager(84): setOutputDevice() setting same device 0 or null device for output 1
01-03 15:07:26.699: V/AudioPolicyManager(84): startOutput() output 1, stream 1, session 1489
01-03 15:07:26.699: V/AudioPolicyManager(84): changeRefCount() stream 1, count 1
01-03 15:07:26.699: V/AudioPolicyManager(84): getDeviceForStrategy() from cache strategy 0, device 2
01-03 15:07:26.699: V/AudioPolicyManager(84): getNewDevice() selected device 2
01-03 15:07:26.699: V/AudioPolicyManager(84): setOutputDevice() output 1 device 2 delayMs 0
01-03 15:07:26.699: V/AudioPolicyManager(84): setOutputDevice() setting same device 2 or null device for output 1
01-03 15:07:26.699: V/AudioPolicyManager(84): releaseOutput() 1
01-03 15:07:26.859: V/AudioPolicyManager(84): stopOutput() output 1, stream 1, session 1489
01-03 15:07:26.859: V/AudioPolicyManager(84): changeRefCount() stream 1, count 0
01-03 15:07:26.859: V/AudioPolicyManager(84): getNewDevice() selected device 0
01-03 15:07:26.859: V/AudioPolicyManager(84): setOutputDevice() output 1 device 0 delayMs 0
01-03 15:07:26.859: V/AudioPolicyManager(84): setOutputDevice() setting same device 0 or null device for output 1
01-03 15:07:27.499: I/StatusBarPolicy(221): BAT. status:5 health:2

1
在你的xml中尝试这个。 <EditText                 android:id =“@ + id / editText1”                 android:layout_width =“match_parent”                 android:layout_height =“wrap_content”                 android:layout_weight =“1”                 android:ems =“10”                 android:inputType =“phone”>                <requestFocus />             </ EditText> - Narendra Pal
这是你在等待的吗? - Narendra Pal
<EditText android:id="@+id/businessPhone_profile" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView4" android:layout_marginBottom="10dp" android:inputType="phone" android:ems="10" android:text="(" /> - user1415135
OnKeyListener 没有正常工作。 - user1415135
好的,我明白了。它在三星MetroPCS上无法工作,其他所有设备都可以正常工作。 - user1415135
好的。我已经在我的答案中添加了它。如果您发现有用的内容,那么您可以接受这个答案。 - Narendra Pal
4个回答

2

如果可以的话,请尝试使用这个库:

http://code.google.com/p/libphonenumber/

或者可以试试这个:

    String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);

这将根据号码所属的国家规则自动格式化该数字。

您还可以使用以下方式在原地格式化可编辑文本:

    PhoneNumberUtils.formatNumber(Editable text, int defaultFormattingType);

请查看 PhoneNumberUtils 以获取更多选项。

1
你可以注册一个文本变更监听器来格式化电话号码。android.telephony package 中有一个内置的监听器。
它很容易使用:
    phoneNumberEditText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

0

在你的xml中尝试这个。

  <EditText android:id="@+id/editText1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:ems="10"
   android:inputType="phone" >
    <requestFocus />
 </EditText>

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接