EditText addTextChangedListener无法正常工作

时间:2018-12-01 06:42:07

标签: android android-studio

我有两个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" />

6 个答案:

答案 0 :(得分:0)

因为您是在onTextChanged中定义文本视图

剪切此行TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);

在addTextChangedListener之前

答案 1 :(得分:0)

尝试一下

  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);
            }
        }
    });

答案 2 :(得分:0)

如下更改代码 在onCreate

中定义
TextView perimeterdetails = (TextView) findViewById(R.id.perimeterdetails);

并在onTextChanged中比较您的字符串,如下所示

if(!TextUtils.isEmpty(s.toString())) {
                perimeterdetails.setText(s);
            }

答案 3 :(得分:0)

尝试

更改条件:

if(!s.equals(“”)){}

if(s.length()!= 0){}

答案 4 :(得分:0)

在Textchanged上未找到ViewById。在添加textchangelistener之前执行此操作。在onTextChange 使用

 if(!TextUtils.isEmpty(s)){
perimeterdetails.setText(s);
}

答案 5 :(得分:0)

尝试一下:)

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.length() <= 0) {

                        perimeterdetails.setText(s);
                    }
                }

});

相关问题