2017-05-05 69 views
0

分離文本我有一個自定義Edittext是從AppCompatEditText延伸並且它顯示像"10.02.2012 10:40"日期文字當用戶點擊一次在文本的任何部分,該部分應被自動選擇。動態地選擇在的EditText

例如:

enter image description here enter image description here

要做到這一點,在我的自定義的EditText,我推翻onSelectonChange

@Override 
    protected void onSelectionChanged(int selStart, int selEnd) { 

     if (isFocused() && isCursorVisible() && isPressed()) { 
      int[] aFoo = findPartOfText(selStart, selEnd); 
      selStart = aFoo [0]; 
      selEnd = aFoo [1]; 
     } 

     super.onSelectionChanged(selStart, selEnd); 

    } 

它不會改變任何東西。我也試過這個:

@Override 
     protected void onSelectionChanged(int selStart, int selEnd) { 
      //Select first 3 characters 
      super.onSelectionChanged(0, 2); 

     } 

結果是一樣的。它也不會選擇前3個字符。

+0

這可以使用onClickListener或OnFocusChangedListener來實現。這裏是鏈接[link](http://stackoverflow.com/questions/9128297/how-to-dynamically-select-text-from-edittext-onclicklistener) –

回答

0

嘗試EditText.setSelection(int start,int end)。當onSelectionChanged被調用時,檢查光標位置或選擇,並調用setSelection來改變它。

+0

setSelection已經調用了SelectionChanged方法。所以調用setSelection方法在onSelectionChanged中沒有任何意義。注:我也嘗試過你的方式。 – ziLk

+0

在onSelectionChanged中,檢查選擇範圍。如果範圍不是你想要的,只調用setSelection。 – jarly