beforeTextChanged,onTextChanged和afterTextChanged没有为Android上的EditText触发

时间:2016-02-28 12:42:03

标签: android android-studio android-edittext

我有一个EditText和OnCreate of Activity,我正在使用以下代码:

EditText totalPriceBox = (EditText)findViewById(R.id.contriTotalPrice);
        totalPriceBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                totalPrice = (totalPrice - Float.parseFloat(s.toString()));
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                totalPrice = (totalPrice + Float.parseFloat(s.toString()));
            }
        });

并将调试器放在所有三种方法中。但没有一个被击中。

我在OnePlus X上测试它,android 5.1.1

Android Studio 1.5.11(在本期1.4之前更新)

如果有任何身体遇到同样的情况并可以提供帮助吗?

修改

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

public class NewCalcActivity extends AppCompatActivity {

    private Float totalPrice = 0f;
    private EditText totalPriceBox;
    private final TextWatcher tw = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //breakpoint placed here
            totalPrice = 10f; // (totalPrice + Float.parseFloat(s.toString()));
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //breakpoint placed here
            totalPrice = 11f; // (totalPrice + Float.parseFloat(s.toString()));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_calc);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        totalPriceBox = (EditText)findViewById(R.id.calcTotalPrice);

        totalPriceBox.addTextChangedListener(tw);
    }

}

布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:padding="0dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_new_contri" android:orientation="vertical"
    tools:context="com.example.sample.NewCalcActivity" >

    <EditText android:id="@+id/calcTotalPrice"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:hint="Title" android:maxLines="5"
        android:inputType="textCapSentences|textMultiLine" android:maxLength="2000"
        android:textColor="@android:color/white" android:minHeight="60dp" />
</LinearLayout>

编辑2:

有一件事我注意到,当我更改方向时,(在更改方向时再次创建活动),TextWatcher会触发断点。

0 个答案:

没有答案