2011-08-27 92 views
6

編輯文字在我的應用我在對話框中輸入中的電話號碼,在編輯文本中自動添加到手機號碼輸入「 - 」例如:999-999-9999這個電話數字格式如何自動格式化電話號碼進入安卓

final EditText text= (EditText)myDialog.findViewById(com.fitzgeraldsoftware.mobitrack.presentationlayer.R.id.Tv2); 
    text.addTextChangedListener(new TextWatcher() { 

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

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

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

         if (keyCode == KeyEvent.KEYCODE_DEL) 
          // Log.v("11111111111111111111","ee"+keyDel); 
          keyDel = 1; 
         return false; 
        } 
       }); 

       if (keyDel == 0) { 

        if (((text.getText().length() + 1) % 4) == 0) 
        { 
         Log.v("11111111111111111111","bb"+((text.getText().length() + 1) % 4)); 
         if (text.getText().toString().split("-").length <= 2) 
         { 
          // Log.v("11111111111111111111","ff"+text.getText().length()); 
          text.setText(text.getText() + "-"); 
          text.setSelection(text.getText().length()); 
         } 
        } 
        Log.v("11111111111111111111","cc"+text.getText().length()); 
        a = text.getText().toString(); 
       } else 
        { 
        Log.v("11111111111111111111","dd"+a); 
        a = text.getText().toString(); 
        keyDel = 0; 
       } 

      } else { 
       Log.v("11111111111111111111","ee"+a); 
       text.setText(a); 
      } 

     } 

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

     } 

     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 


    }); 

輸出爲:999-999-999 如何處理精確的輸出爲999-999-9999(3位數,3位數-4digits)

請提出了一些解決方案。

回答

0

見你可以使用這個庫調用International phone input for Android'託管在GitHub上。

它真棒,實現onValidityChangeListener

  • 自動格式化爲用戶鍵入數
  • 輸入佔位符自動設置的例子數爲 所選國家
  • 從下拉列表中選擇一個國家將更新撥號代碼在 輸入
  • 鍵入一個不同的撥號代碼將自動更新顯示 標誌
  • 輕鬆嵌入一個自定義視圖
  • 監聽器可用於檢測有效性變化
  • 自動檢測電話號碼時可用
  • 信息聽「完成」,即使在鍵盤上

    //Including in layout <net.rimoto.intlphoneinput.IntlPhoneInput android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/my_phone_input" />

0

只是,添加此textChangedListener到您的edit_text

 UsPhoneNumberFormatter addLineNumberFormatter = new UsPhoneNumberFormatter(
      new WeakReference<EditText>(etPhone)); 
      edit_text.addTextChangedListener(addLineNumberFormatter); 

UsPhoneNumberForma tter是一類,它的擴展TextWatcher

class UsPhoneNumberFormatter implements TextWatcher { 

//This TextWatcher sub-class formats entered numbers as 1 (123) 456-7890 
private boolean mFormatting; // this is a flag which prevents the 
           // stack(onTextChanged) 
private boolean clearFlag; 
private int mLastStartLocation; 
private String mLastBeforeText; 
private WeakReference<EditText> mWeakEditText; 

public UsPhoneNumberFormatter(WeakReference<EditText> weakEditText) { 
    this.mWeakEditText = weakEditText; 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count, 
     int after) { 
    if (after == 0 && s.toString().equals("1 ")) { 
     clearFlag = true; 
    } 
    mLastStartLocation = start; 
    mLastBeforeText = s.toString(); 
} 

@Override 
public void onTextChanged(CharSequence s, int start, int before, 
     int count) { 
    // TODO: Do nothing 
} 

@Override 
public void afterTextChanged(Editable s) { 
    // Make sure to ignore calls to afterTextChanged caused by the work 
    // done below 
    if (!mFormatting) { 
     mFormatting = true; 
     int curPos = mLastStartLocation; 
     String beforeValue = mLastBeforeText; 
     String currentValue = s.toString(); 
     String formattedValue = formatUsNumber(s); 
     if (currentValue.length() > beforeValue.length()) { 
      int setCusorPos = formattedValue.length() 
        - (beforeValue.length() - curPos); 
      mWeakEditText.get().setSelection(setCusorPos < 0 ? 0 : setCusorPos); 
     } else { 
      int setCusorPos = formattedValue.length() 
        - (currentValue.length() - curPos); 
      if(setCusorPos > 0 && !Character.isDigit(formattedValue.charAt(setCusorPos -1))){ 
       setCusorPos--; 
      } 
      mWeakEditText.get().setSelection(setCusorPos < 0 ? 0 : setCusorPos); 
     } 
     mFormatting = false; 
    } 
} 

private String formatUsNumber(Editable text) { 
    StringBuilder formattedString = new StringBuilder(); 
    // Remove everything except digits 
    int p = 0; 
    while (p < text.length()) { 
     char ch = text.charAt(p); 
     if (!Character.isDigit(ch)) { 
      text.delete(p, p + 1); 
     } else { 
      p++; 
     } 
    } 
    // Now only digits are remaining 
    String allDigitString = text.toString(); 

    int totalDigitCount = allDigitString.length(); 

    if (totalDigitCount == 0 
      || (totalDigitCount > 10 && !allDigitString.startsWith("1")) 
      || totalDigitCount > 11) { 
     // May be the total length of input length is greater than the 
     // expected value so we'll remove all formatting 
     text.clear(); 
     text.append(allDigitString); 
     return allDigitString; 
    } 
    int alreadyPlacedDigitCount = 0; 
    // Only '1' is remaining and user pressed backspace and so we clear 
    // the edit text. 
    if (allDigitString.equals("1") && clearFlag) { 
     text.clear(); 
     clearFlag = false; 
     return ""; 
    } 
    if (allDigitString.startsWith("1")) { 
     formattedString.append("1 "); 
     alreadyPlacedDigitCount++; 
    } 
    // The first 3 numbers beyond '1' must be enclosed in brackets "()" 
    if (totalDigitCount - alreadyPlacedDigitCount > 3) { 
     formattedString.append("(" 
       + allDigitString.substring(alreadyPlacedDigitCount, 
         alreadyPlacedDigitCount + 3) + ") "); 
     alreadyPlacedDigitCount += 3; 
    } 
    // There must be a '-' inserted after the next 3 numbers 
    if (totalDigitCount - alreadyPlacedDigitCount > 3) { 
     formattedString.append(allDigitString.substring(
       alreadyPlacedDigitCount, alreadyPlacedDigitCount + 3) 
       + "-"); 
     alreadyPlacedDigitCount += 3; 
    } 
    // All the required formatting is done so we'll just copy the 
    // remaining digits. 
    if (totalDigitCount > alreadyPlacedDigitCount) { 
     formattedString.append(allDigitString 
       .substring(alreadyPlacedDigitCount)); 
    } 

    text.clear(); 
    text.append(formattedString.toString()); 
    return formattedString.toString(); 
} 
} 

了把這樣的格式

999-999-9999