gpt4 book ai didi

android - EditText.getText().toString() 有时会返回 ""

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:19 25 4
gpt4 key购买 nike

我在 TextWatcheronTextChanged() 方法中获取 EditText 的文本及其长度。
当我输入添加字符时它工作正常,但是从文本中删除字符时 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>

代码:

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() 方法。但这并没有解决问题。

最佳答案

我没有发现任何问题。 from_location 的文本应该改变,它确实改变了。

当您在 TextWatcher 代码中时,为什么要“检查”EditText (from_location)。我认为 EditText 的值在变化时不必“稳定”。

可能发生的情况是,当您检查 from_location 中的 CharSequence 是否正在更新时,有时您会在更改过程中发现它,有时会在更改之后发现它。

您是否检查过 (CharSequence s, int start, int before, int count) 是否返回预期值?

如我所见。如果您想对文本进行任何更改,您应该在 afterTextChanged 方法的 String s 参数上进行更改。

我写这段代码是为了检查在更改 EditText 的内容时发生了什么,也许它是任何

    mEdtText.addTextChangedListener(new TextWatcher() {
private static final String TAG = "TextWatcher";
private static final boolean DEBUG = true;

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (DEBUG)
Log.d(TAG,
"(onTextChanged) In \"" + s + "\" " + before
+ " characters "
+ " are being replaced at " + start
+ " by " + count + " ("
+ s.subSequence(start, start + count) + ")");
if (DEBUG)
Log.d(TAG,
"(onTextChanged) mEdtText: \"" + mEdtText.getText()
+ "\"");
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (DEBUG)
Log.d(TAG,
"(beforeTextChanged) In \"" + s + "\" " + count
+ " characters ("
+ s.subSequence(start, start + count)
+ ") are about to be replaced at " + start
+ " by " + after);
if (DEBUG)
Log.d(TAG,
"(beforeTextChanged) mEdtText: \""
+ mEdtText.getText() + "\"");

}
}

如果查看 EditText 的源代码,您会发现它继承了 TextViewgetText 方法,并返回 CharSequence多行文本。您可以为另一个 CharSequence mTransformed 定义种子。在巨大的 setText 方法中,有一个时刻 mTextmTransformed 取代。可能当您调用 from_locationgetText 时,您将获得 mText 并且在 setText 踢完之后第二次执行此操作输入并得到 mTransformed 作为答案。

如果你想检查这个只需改变

CharSequence cs = from_location.getText();
Log.d(... + cs + ...); // no need to call toString as implicit call.
...
Log.d(... + cs.length() + ...);

关于android - EditText.getText().toString() 有时会返回 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17994263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com