我可以在不同的 EditText 小部件上使用相同的 TextWatcher

Can I use The same TextWatcher on different EditText widgets

我有一个表格。当用户在某些 int 字段中输入不同的值时,我想在 "live time" 中显示一个计算。我让 Activity 实现了 TextWatcher 接口并在 3 个不同的 EditText 字段上设置了一个侦听器,但看起来 Textwatcher 只检测到 Activity 代码中声明的第一个 EditText。

您可以从下面的代码中看到,我试图抓取一些字段,将它们转换为整数并在表单底部的 EditText 字段中显示输出。我哪里错了?我是否需要在所有涉及的 EditText 字段上单独实施 textWatcher?这将是一个相当冗长的解决方案,使我的代码比我想要的要长很多

public class NewStageFormActivity extends AppCompatActivity implements TextWatcher{

    Context mContext;

    EditText mStageName, mPaperTargets, mHitsPerTarget, mSteelTargets, mSteelNPMs, mOutput;
    Spinner mScoringType, mStrings;
    CheckBox mNoShoots, mNPMs;
    Button mSaveButton;
    Match mGlobalMatch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stage_form);

        mContext = this;
        mGlobalMatch = GlobalMatch.getMatch();

        mStageName = (EditText)findViewById(R.id.stage_name_et);
        mPaperTargets = (EditText)findViewById(R.id.paper_targets_et);
        mHitsPerTarget = (EditText)findViewById(R.id.hits_per_target_et);
        mSteelTargets = (EditText)findViewById(R.id.steel_targets_et);
        mSteelNPMs = (EditText)findViewById(R.id.steel_npm_et);
        mScoringType = (Spinner)findViewById(R.id.scoring_type_spinner);
        mStrings = (Spinner)findViewById(R.id.strings_spinner);
        mNoShoots = (CheckBox)findViewById(R.id.no_shoots_cb);
        mNPMs = (CheckBox)findViewById(R.id.npm_cb);
        mSaveButton = (Button)findViewById(R.id.save_button);
        mOutput = (EditText)findViewById(R.id.output_et);

        // paper * hitsPer + steel
        mPaperTargets.addTextChangedListener(this);
        mSteelTargets.addTextChangedListener(this);
        mSteelTargets.addTextChangedListener(this);

        mSaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mStageName.getText().toString().equals("") || mPaperTargets.getText().toString().equals("") ||
                        mHitsPerTarget.getText().toString().equals("") || mSteelTargets.getText().toString().equals("") ||
                        mSteelNPMs.getText().toString().equals("")){
                    Toast.makeText(mContext, "You must fill in all form fields", Toast.LENGTH_SHORT).show();
                } else {
                    String name = mStageName.getText().toString();
                    String type = mScoringType.getSelectedItem().toString();
                    int strings = Integer.valueOf(mStrings.getSelectedItem().toString());
                    int paperTargets = Integer.valueOf(mPaperTargets.getText().toString());
                    int hitsPerTarget = Integer.valueOf(mHitsPerTarget.getText().toString());
                    boolean noShoots;
                    boolean npms;
                    if(mNoShoots.isChecked()){
                        noShoots = true;
                    } else {
                        noShoots = false;
                    }
                    if(mNPMs.isChecked()){
                        npms = true;
                    } else {
                        npms = false;
                    }
                    int steelTargets = Integer.valueOf(mSteelTargets.getText().toString());
                    int steelNPMs = Integer.valueOf(mSteelNPMs.getText().toString());

                    MatchStage matchStage = new MatchStage(name, type, strings, paperTargets, hitsPerTarget,
                            noShoots, npms, steelTargets, steelNPMs);

                    mGlobalMatch.getStages().add(matchStage);

                    String jsonString = new Gson().toJson(mGlobalMatch);
                    MatchHelper.updateFile(mContext, MatchHelper.createFileName(mGlobalMatch.getMatchId()),
                            jsonString);

                    Intent intent = new Intent(mContext, StagesListActivity.class);
                    startActivity(intent);
                }
            }
        });


    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        int paper = Integer.valueOf(mPaperTargets.getText().toString());
        int hitsPer = Integer.valueOf(mHitsPerTarget.getText().toString());
        int steel = Integer.valueOf(mSteelTargets.getText().toString());

        int minRound = (paper * hitsPer) + steel;
        int points = minRound * 5;

        mOutput.setText("Minimum rounds: " + (minRound) + "\t\t Points: " + points);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

根据我的经验,我在多个 EditText 上使用相同的 TextWatcher 时遇到过问题。您可以通过创建一个方法来解决这个问题,该方法为每个 EditText 生成一个新的 TextWatcher 实例,这样您的代码就不会很冗长。尝试这样的事情:

为您的 Activity 删除 implements TextWatcher 及其相关方法。实现方法 createTextWatcher():

private TextWatcher createTextWatcher() {
    return new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            // Copy your original code
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            // Copy your original code
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // Copy your original code
        }
    };   
}

现在像这样使用它:

mPaperTargets.addTextChangedListener(createTextWatcher());
mSteelTargets.addTextChangedListener(createTextWatcher());

@Natan Felipe 的 link 上投票最多的答案还演示了每个 EditText.

使用一个 TextWatcher 实例