Gee*_*eek 7 null android gettext android-edittext
我得到了EditText的文本及其长度onTextChanged()方法TextWatcher.
当我键入以添加字符时,它工作正常,但getText()即使文本不为空,从文本中删除字符也会为空.这是随机发生的,而不是每次我删除字符.我观察到这种情况主要发生在文本中有3-4个字符时我按退格键.
奇怪的是,这个问题只发生在设备上,而不是模拟器上.
布局文件:
<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>
Run Code Online (Sandbox Code Playgroud)
代码:
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());
}
});
Run Code Online (Sandbox Code Playgroud)
注意:我尝试使用 afterTextChanged()和beforeTextChanged()方法.但这并没有解决问题.
如果您正在经历调试模式,请记住它有两个存储值的位置。在我的例子中,EditText有mText(Spannable String Builder),它的子字段(也是mText)是带有ID的字段。可跨越字符串生成器将返回 .toString() 的值。另一种将返回 .getText() 的值。
| 归档时间: |
|
| 查看次数: |
5331 次 |
| 最近记录: |