2011-08-09 26 views
0

我正在創建一個應用程序,我希望該用戶可以在運行時輸入IP地址。 其中我設計了4個編輯文本,用戶可以輸入IP地址。我爲它創建了XML。 現在我創建了一個擴展TextWatcher的自定義類來爲EditText設置addTextChangedListener。編輯文本最初沒有輸入點擊Android

 ipSlotFirst=(EditText)ipSettings.findViewById(R.id.ip_slot1); 
     ipSlotFirst.setFilters(FilterArray); 
     CustomOnTextChangeListener slot1=new CustomOnTextChangeListener(ipSlotFirst); 
     ipSlotFirst.addTextChangedListener(slot1); 
     ipSlotSecond=(EditText)ipSettings.findViewById(R.id.ip_slot2); 
     ipSlotSecond.setFilters(FilterArray); 
     CustomOnTextChangeListener slot2=new CustomOnTextChangeListener(ipSlotSecond); 
     ipSlotSecond.addTextChangedListener(slot2); 
     ipSlotThird=(EditText)ipSettings.findViewById(R.id.ip_slot3); 
     ipSlotThird.setFilters(FilterArray); 
     CustomOnTextChangeListener slot3=new CustomOnTextChangeListener(ipSlotThird); 
     ipSlotThird.addTextChangedListener(slot3); 
     ipSlotFourth=(EditText)ipSettings.findViewById(R.id.ip_slot4); 
     ipSlotFourth.setFilters(FilterArray); 
     CustomOnTextChangeListener slot4=new  CustomOnTextChangeListener(ipSlotFourth); 
     ipSlotFourth.addTextChangedListener(slot4); 

現在我想,當用戶輸入三位在一個EditText上它會自動轉移到下一個編輯文本,當用戶刪除EDITTEXT的內容和重點轉移到前面的編輯​​文本一次編輯的內容文本變成空的。我用一個customOnTextChangeListener類做了它。

class CustomOnTextChangeListener implements TextWatcher{ 

    /** The m focused view. */ 
    EditText mFocusedView; 

    /** 
    * Instantiates a new custom on text change listener. 
    * 
    * @param pFocusedView the focused view 
    */ 
    public CustomOnTextChangeListener(EditText pFocusedView){ 
     mFocusedView = pFocusedView; 
    } 
    @Override 
    public void afterTextChanged(Editable s) { 
    } 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     if(mFocusedView.equals(ipSlotFirst)){ 
      if(s.length()>=3){ 
       ipSlotSecond.requestFocus(); 
      } 
     }else if(mFocusedView.equals(ipSlotSecond)){ 
      if(s.length()>=3){ 
       ipSlotThird.requestFocus(); 
      }else if(s.length()==0){ 
       ipSlotFirst.requestFocus(); 
      } 
     } 
     else if(mFocusedView.equals(ipSlotThird)){ 
       if(s.length()>=3){ 
        ipSlotFourth.requestFocus(); 
       }else if(s.length()==0){ 
        ipSlotSecond.requestFocus(); 
       } 

      }else if(mFocusedView.equals(ipSlotFourth)){ 
       if(s.length()>=3){ 
        portNumber.requestFocus(); 
       }else if(s.length()==0){ 
        ipSlotThird.requestFocus(); 
       } 


     } 
    } 
} 

一切工作正常。但是當我在第一個編輯文本中輸入192這樣的IP時,我遇到了一個問題,然後它自動轉換到第二個編輯文本,然後我輸入了10,然後點擊第三個編輯文本,然後點擊一個數字,然後第一次編輯文本沒有進行輸入。

請幫忙在這方面。

問候

迪帕克·戈埃爾

+1

這種類型的點擊對焦的手動移動嘗試在** ** afterTextChanged事件編寫代碼 – Hanry

+0

檢查我編輯的答案 – ngesh

+0

謝謝雅爾這對我很有用 –

回答

0
if(mFocusedView.equals(ipSlotFirst)){ 
     if(s.length()>3){ 
      ipSlotSecond.requestFocus(); 
     } 

添加上面,而不是

if(mFocusedView.equals(ipSlotFirst)){ 
     if(s.length()>=3){ 
      ipSlotSecond.requestFocus(); 
     } 
+0

感謝您的回覆。但這不會幫助,因爲當我把IP船尾呃三位數字編輯文本它不會自動將焦點轉移到第四編輯文本 –

+0

它仍然表現相同。但hanry的答案適用於我。 –