android - EditText addTextChangedListener 无法正常工作
问题描述
我有两个 EditText 输入,当计算后文本发生变化时,我必须在同一活动中的 textView 上显示结果。addTextChangedListener 不适合我
Activity.java code:
width.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.equals("") ) {
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
perimeterdetails.setText(s);
}
}
});
XML 代码:
<EditText
android:id="@+id/width"
android:layout_width="0dp"
android:background="@drawable/edittextbackground"
android:layout_weight="1"
android:inputType="numberDecimal"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/perimeterdetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
解决方案
如下更改您的代码onCreate
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
并在 onTextChanged 中比较您的字符串,如下所示
if(!TextUtils.isEmpty(s.toString())) {
perimeterdetails.setText(s);
}
推荐阅读
- css - 有没有工具可以告诉我是否有未使用的 CSS 类?
- python - TensorFlow 填充实现
- python - 如何从 python 列表中生成具有给定值的键:值列表
- google-bigquery - Google Big Query:浏览器 SQL 编辑器的替代品?
- php - PHP - 用嵌套循环关闭 div
- javascript - 如何从数组 FIRESTORE 中获取值
- xcode - 升级到 xcode 10 后出现问题 - 找不到文件
- python - Python:尝试写入文件时出现“ValueError:无法打开控制台输入缓冲区进行写入”
- amazon-web-services - Apache Spark/AWS EMR 和处理文件的跟踪
- hadoop - hbase master 没有启动