- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 TextWatcher
的 onTextChanged()
方法中获取 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
的源代码,您会发现它继承了 TextView
的 getText
方法,并返回 CharSequence多行文本
。您可以为另一个 CharSequence mTransformed
定义种子。在巨大的 setText
方法中,有一个时刻 mText
被 mTransformed
取代。可能当您调用 from_location
的 getText
时,您将获得 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/
正在阅读 Underscore.js 以了解它的 is[String|Number|...] 方法是如何工作的,现在我很困惑。下划线: toString.call(obj) == ['object '
scala> Array(1, 2, 3).toString res1: String = [I@11cf437c scala> List(1, 2, 3).toString res2: String
我在将字符串从 stringbuilder 转换为字符串时遇到问题。问题类似于 this issue但略有不同: 这是我的简化代码: StringBuilder sb = new StringBuil
我正在尝试将从正在构建的搜索功能中名为 Part 的模型返回的 int id 转换为字符串,以便简化搜索。 这是我目前使用的 if 语句: if(part.getId().toString().ind
我需要从所选内容中提取文本并将其发送到 TTS 服务。 TTS 服务将返回一个流 URL 和每个单词的一组索引,指示它们的开始和结束位置(时间和文本)。 当用户播放流时,我想在读出每个单词时突出显示它
我想知道人们在 Java 的 toString() 方法中放入了什么。 我一直在向一些新类添加一些内容,并且想知道它是否应该包含类名。 在类ClassConfig中,我无法决定是否应该拥有 @Over
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 下面是我的主要方法,其中比较两个对象引用。覆盖toString()方法
我的问题是,JAVA中没有提供toString()方法的类是否可以打印出特定信息? 问题在于:我们为我们的应用程序提供了一个记录器(使用aspectJ),它打印出给出的特定参数。例如: public
基本上这就是我想要实现的目标。 classname@address(?)[original toString()], object's name, object's age @Override pub
据我所知,Scala 中的中缀运算符的使用应该等同于方法的调用。所以: scala> "a" + 3.toString res0: java.lang.String = a3 是相同的: scala>
这个问题已经有答案了: Why can't I access a property of an integer with a single dot? (5 个回答) 已关闭 7 年前。 functio
我正在进行测试,并且给出了很多单元(隐藏)测试,但是我的一段代码遇到了这个错误。大家能帮帮我吗? getString(comment) { const authorName = comment.get
return toString.call(obj) 和 return obj.toString() 有什么区别? 我通常会找到具有这些不同风格的代码 最佳答案 toString.call(obj) 返
例如,我必须在每个数字到字符串的转换中使用 .ToString(CultureInfo.CurrentCulture)。我能否以某种方式重写 .ToString(),这样我就不会在字符串转换中显式地收
var d = []; console.log(typeof d); // weird! console.log(d.toString()); //Prints nothing since there
当对象字面量调用toString()方法如{}.toString()会导致语法错误,但是当数组字面量调用toString()没关系。当我将对象文字分配给一个变量时,当它调用 toString() 方法
我在打印特殊数组时遇到问题: 我使用 System.out.println(Arrays.toString()); 打印多个对象的数组但现在数组中充满了对象,这些对象具有 char 值,我想打印分配给
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
> ~0..toString(2) -1 > ~1..toString(2) -2 > ~2..toString(2) -11 > ~3..toString(2) -12 > (~1).toStrin
这是我的问题,我的机器使用法语文化,因此默认情况下它以法语方式解析 (3,141592)。 如果机器文化不是美国,这里是重现我的问题的代码: float number = 4103.26808
我是一名优秀的程序员,十分优秀!