2015-01-19 2 views
2

이상한 점이 있습니다. 사용자가 EditText 필드에 입력하는 동안 일부 텍스트의 서식을 지정하려고합니다. TextWatcher를 사용하고 afterTextChanged 이벤트 (아래 코드)에 응답합니다. 홀수는이 논리가 처음 실행 된 후 텍스트가 역전되기 시작합니다. 편집 가능한 객체에는 문자열이 거꾸로 들어있는 것으로 보입니다. 누구든지이 문제를 해결하는 방법을 알고 있습니까?EditText 값이 반대로됩니다.

_textWatcher = new TextWatcher() 
       { 
        @Override 
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) 
        { 
        } 

        @Override 
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) 
        { 
        } 

        @Override 
        public void afterTextChanged(Editable editable) 
        { 
         Editable e = textView.getText(); 
         String text = e.toString(); 

         logger.d("[FormListAdapter][addTextChangedListener] Format Text: " + text); 

         Number numVal = FormFieldBusiness.ConvertStringToNumber(text, formField.GetFormFieldType()); 
         String fText = FormFieldBusiness.GetFormattedValue(numVal, formField.GetFormFieldType()); 

         editText.removeTextChangedListener(_textWatcher); 
         textView.setText(text); 
         editText.addTextChangedListener(_textWatcher); 
        } 
       }; 

       editText.addTextChangedListener(_textWatcher); 

* 여기에서 찾고 내 XML 레이아웃 파일입니다있어 거기에 사람을 도와 시도에서 UPDATE *

.

앞서 언급했듯이 텍스트는 처음부터 setText 메서드가 호출 될 때까지 정확합니다. 그런 다음 편집 가능한 객체가 반대로 바뀝니다.

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="120dp"> 

    <!-- Top Layout --> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="60dp" 
     android:weightSum="10" 
     android:id="@+id/TopLayout"> 

     <TextView 
      android:id="@+id/TitleTextView" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="7" 
      android:gravity="center_vertical" 
      android:text="Headling Rent (pa)" 
      android:textSize="16dp" 
      style="@style/KelFormListItemLabel" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="3" 
      android:weightSum="2" 
      android:background="@color/formListItemValueBackgroundColor"> 

      <ImageView 
       android:id="@+id/CurrencyIconView" 
       android:layout_width="22dp" 
       android:layout_height="22dp" 
       android:layout_weight="0" 
       android:src="@drawable/icon_currency_pound" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="5dp" /> 

      <EditText 
       android:id="@+id/ValueTextView" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:gravity="right|center_vertical" 
       android:text="20,000" 
       android:textSize="16dp" 
       android:inputType="number" 
       android:selectAllOnFocus="true" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:textIsSelectable="true" 
       style="@style/KelFormListItemValue"/> 

     </LinearLayout> 

    </LinearLayout> 

    <!-- Bottom Layout --> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="60dp" 
     android:weightSum="10" 
     android:id="@+id/BottomLayout" 
     android:layout_below="@+id/TopLayout"> 

     <TextView 
      android:id="@+id/BottomTitleTextView" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="7" 
      android:gravity="center_vertical" 
      android:text="Headling Rent (pa)" 
      android:textSize="16dp" 
      style="@style/KelFormListItemLabel" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="3" 
      android:weightSum="2" 
      android:background="@color/formListItemValueBackgroundColor"> 

      <ImageView 
       android:id="@+id/BottomCurrencyIconView" 
       android:layout_width="22dp" 
       android:layout_height="22dp" 
       android:layout_weight="0" 
       android:src="@drawable/icon_currency_pound" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="5dp" /> 

      <EditText 
       android:id="@+id/BottomValueTextView" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="2" 
       android:gravity="right|center_vertical" 
       android:text="20,000" 
       android:textSize="16dp" 
       android:inputType="number" 
       android:selectAllOnFocus="true" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:textIsSelectable="true" 
       style="@style/KelFormListItemValue"/> 

     </LinearLayout> 

    </LinearLayout> 
</RelativeLayout> 
+0

아래 표를 설명하는 댓글을 게시하지 않은 이유는 무엇입니까? 텍스트가 왜 바뀌는 지 알아내는 데 도움이 필요합니다. 나는 관련성이있는 모든 코드를 게시했는데 왜 내가 투표를하지 않았는지 확신 할 수 없었다. –

+0

심지어 upvoting까지. afterTextChanged에서 일어나는 일들을 통해 우리를 실행할 수 있습니까? [documentation] (http://developer.android.com/reference/android/text/TextWatcher.html)을 읽으면서 텍스트에 적용하려는 모든 변경은 onTextChanged에서 수행되어야한다고 생각합니다. – josephus

+0

afterTextChanged 이벤트의 모든 논리는 문자열을 숫자로 변환 한 다음 필드 유형 (예 : 정수, 퍼센트, 통화 등)에 따라 형식을 지정한 다음 서식있는 문자열을 EditText보기로 설정합니다. 내가 확인한 내용과 변환 논리가 텍스트를 뒤집는 것이 아닙니다. 올바른 순서로 setText에 들어가고 afterTextChanged 이벤트가 다시 호출 될 때 반대가됩니다. onTextChanged 이벤트에서도이 작업을 시도했으며 같은 결과를 얻었습니다. –

답변

3

듣고있는 EditText 필드에 텍스트를 설정하고 있습니다 (청취자를 제거 했음에도 불구하고). 이로 인해 커서가 줄의 시작 부분으로 이동하므로 뒤로 유형이 변경됩니다.

TextWatcher에서 TextView.OnEditorActionListener로 변경하고 EditText에 설정된 동작 키를 수신 대기하므로 동작 키를 누를 때, 원하는 기능을 수행합니다.

당신의 버튼을 XML로

android:imeOptions="actionDone" 

를 추가하고 선택적으로

TextView.OnEditorActionListener editingActionListener = new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if ((actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE) { 
      //do stuff where you set the text here 
     } 
     return true; 
    } 
}; 
editText.setOnEditorActionListener(editingActionListener); 

에 할당, 당신은

View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (v.getClass().equals(EditText.class)) { 
      if (!hasFocus) { 
       //do your stuff here 
      } 
     } 
    } 
}; 
editText.setOnFocusChangeListener(focusChangeListener); 
+0

나는이 문제를 이미 고쳤으며 정확히 무엇을했는지 확신 할 수 없다. 응답은 의미가있다. –

+0

@ miken.mkndev 어떻게 해결했는지 되돌아 보면 커뮤니티에 커다란 도움이된다. , 답으로 게시하십시오. 당신은 당신의 자신의 대답을 받아 들일 수 있습니다, 게다가 당신은 내 upvote 얻을거야 :) – josephus

1

단순히 올바른 위치로 커서를 이동 OnFocusChangeListener 사용할 수 있습니다 EditText 내용을 설정 한 후 TextWatcher :

@Override 
public void afterTextChanged(Editable editable) 
{ 
    ... 
    int position = editText.getSelectionStart(); 
    editText.setText(text); 
    editText.setSelection(position); 
    ... 
}