我可以在不同的 EditText 小部件上使用相同的文本观察器吗?



我有一个表格。我想在"实时"中显示一个计算,当用户在某些 int 字段中输入不同的值时。我让我的活动实现了文本观察器接口,并在 3 个不同的 EditText 字段上设置了一个侦听器,但似乎文本观察器只检测活动代码中声明的第一个 EditText。

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

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) + "tt 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的链接上投票最多的答案还演示了每EditText使用一个TextWatcher实例。

相关内容

  • 没有找到相关文章

最新更新