EditText值相反

时间:2015-01-19 17:51:08

标签: android android-edittext textwatcher

我有一个奇怪的。当用户键入EditText字段时,我尝试格式化一些文本。我正在使用TextWatcher并响应afterTextChanged事件(下面的代码)。奇怪的是,在第一次运行此逻辑后,文本开始变为反转。似乎Editable对象包含向后的字符串。有没有人知道如何解决这个问题?

_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布局文件。

正如我之前提到的,文本是正确的,直到第一次调用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>

2 个答案:

答案 0 :(得分:3)

问题是您正在设置您正在侦听的EditText字段中的文本(即使您已删除了侦听器)。这导致光标移动到行的开头,因此使其向后键入

尝试从TextWatcher更改为TextView.OnEditorActionListener并侦听EditText上设置的操作键,以便当他们点击操作键时,它会执行您想要的功能。

添加

android:imeOptions="actionDone"

到你的按钮xml并用

指定它
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);

或者,您可以使用OnFocusChangeListener

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);

答案 1 :(得分:2)

设置EditText内容后,只需将光标移动到TextWatcher中的正确位置:

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