我有两个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" />发布于 2018-12-01 14:57:53
按如下所示更改代码在onCreate中定义此代码
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);在onTextChanged中比较你的字符串,如下所示
if(!TextUtils.isEmpty(s.toString())) {
perimeterdetails.setText(s);
}发布于 2018-12-01 14:47:44
因为您是在onTextChanged中定义文本视图
剪切此行TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
在addTextChangedListener之前
发布于 2018-12-01 14:54:54
尝尝这个
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);
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("") ) {
perimeterdetails.setText(s);
}
}
});https://stackoverflow.com/questions/53568438
复制相似问题