0

我的要求是,用戶應該只能輸入數字0到9,並且每4個字符後,一個「 - 」符號會自動附加到edittext.user應該除了結束之外,不能刪除edittext的任何部分。請建議鋤頭這樣做。創建一個自定義輸入類型爲android

用戶不應該能夠將光標移動到鍵入文本中間的任何位置,並且能夠將其刪除。如何做到這一點?當用戶移動光標位置時會調用什麼事件?

回答

2

您的第一個要求是0-9是通過在XML用戶類型編號中設置編輯文本屬性來實現的 並在編輯文本對象和計數字中設置文本觀察器偵聽器中的文本數並在那裏您可以追加「 - 」字符。

+0

如何禁用從文本的結尾將光標移動到在輸入屏幕中間的任何地方? – rDroid 2011-04-06 07:24:07

+0

文字變更觀察員做了工作! – rDroid 2011-11-23 09:10:48

0

我找到了解決我的問題的方法:用戶不應該能夠將光標移動到鍵入文本中間的任何位置。我們需要擴展EditText並添加重寫以下功能:

@Override 
protected void onSelectionChanged(int selStart, int selEnd) { 
    // TODO Auto-generated method stub 
    // this method will check if the cursor is moved. if yes then bring back 
    // the cursor to the end so that the user cannot delete anythign from 
    // the middle of the text(here sub id). Any editing will only be 
    // possible at the end of the text 
    if (selStart == selEnd) { 
     int length = getText().toString().length(); 
     setSelection(length); 
    } 
    super.onSelectionChanged(selStart, selEnd); 
} 
相關問題