Я получаю текст EditText и его длину в onTextChanged()
методе TextWatcher
.
Он отлично работает, когда я печатаю, чтобы добавлять символы, но при удалении символов из текста getText()
пуст, даже если текст не пуст. Это происходит случайным образом, а не каждый раз, когда я удаляю символы. Я наблюдал, что это происходит в основном, когда в тексте есть 3-4 символа, и я нажимаю backspace.EditText.getText(). ToString() иногда возвращает ""
Странная часть заключается в том, что эта проблема возникает только на устройстве, а не на эмуляторе.
Макет файла:
<LinearLayout
style="@style/create_trip_activity_components_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<EditText
android:id="@+id/from_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:ems="10"
android:hint="@string/from_location_hint"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/use_current_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="useCurrentLocation"
android:text="@string/use_current"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Код:
fromLocation.addTextChangedListener(new TextWatcher() {
@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 (fromLocation == null) {
Log.d(TAG, "fromLocation is null................");
}
Log.d(TAG, "fromLocation text : "
+ fromLocation.getText().toString());
Log.d(TAG, "fromLocation text length : "
+ fromLocation.getText().length());
Log.d(TAG, "S : "
+ s);
Log.d(TAG, "S length : "
+ s.length());
}
});
Примечание: Я попытался с помощью afterTextChanged()
и beforeTextChanged()
методов. Но это не решило проблему.
редактируемые является CharSequence поэтому нет необходимости, чтобы преобразовать его в строку для вызова длины(). Просто говорю. Попробуйте выйти из AfterTextChanged (Редактирование s). –
@DoctororDrive Пробовал в 'afterTextChanged()'. Еще одна проблема. – Geek
может показать, как вы удаляете персонажа? – Sameer