猿问

如何使用文本观察器为表单中的多个字段更新进度条?

我有一堆字段:名字、姓氏、电子邮件、密码,每次有有效条目时我都需要更新进度条。因此,例如,如果填写了电子邮件地址和姓氏,ProgressBar则应为 50%。用户可以按任何顺序输入字段,但ProgressBar应始终显示更新的进度级别。我正在使用addTextChangeListener(Text Watcher).


firstName.addTextChangedListener(new TextWatcher() {

        @Override

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }


        @Override

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

            int length = s.toString().trim().length();

            if (length > 0) {

                registrationProgress.setProgress(registrationProgress.getProgress() + 25);

            } else {

                registrationProgress.setProgress(registrationProgress.getProgress() - 25);

            }


        }


        @Override

        public void afterTextChanged(Editable s) {


        }

    });

当我运行上面的代码时,我会ProgressBar继续为每个文本条目添加 25,而不是在填充整个字段并且用户从该字段中取出时。我做错了什么,我该怎么办?


眼眸繁星
浏览 148回答 3
3回答

UYOU

当我运行上面的程序时,我会看到进度条继续在每个文本条目中添加 25 [...] 我做错了什么?TextWatcher 的 onTextChanged、beforeTextChanged 和 afterTextChanged 之间的差异将帮助您了解为什么您的应用会出现这种行为。我该怎么办?您可以覆盖afterTextChanged()而不是onTextChanged()因为您对用户键入字母(或按退格键)的频率不感兴趣。在 中afterTextChanged(),您评估到目前为止String已输入的 的长度EditText。为了跟踪进度,我想介绍一个private SparseBooleanArray isTextComplete = new SparseBooleanArray();以及一种方法private void checkProgress(){&nbsp; &nbsp; int progress = 0;&nbsp; &nbsp; for(int i = 0; i <&nbsp; isTextComplete.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; if(isTextComplete.valueAt(i)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progress++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; registrationProgress.setProgress(progress * 25);}在 中afterTextChanged(),您可以更新SparseBooleanArray并调用checkProgress():@Overridepublic void afterTextChanged(Editable s) {&nbsp; &nbsp; int len = etMail.getText().toString().length();&nbsp; &nbsp; if(len > 0){&nbsp; &nbsp; &nbsp; &nbsp; isTextComplete.put(etMail.getId(), true);&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; isTextComplete.put(etMail.getId(), false);&nbsp; &nbsp; }&nbsp; &nbsp; checkProgress();}

当年话下

当我运行上面的时候,我得到了进度条,继续为每个文本添加 25这是因为您正在使用以下代码(请阅读评论):@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {&nbsp; &nbsp; int length = s.toString().trim().length();&nbsp; &nbsp; // if string not empty, keep adding the progress with 25&nbsp; &nbsp; if (length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; registrationProgress.setProgress(registrationProgress.getProgress() + 25);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; // subtract 25 each time string empty.&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; registrationProgress.setProgress(registrationProgress.getProgress() - 25);&nbsp; &nbsp; }}你可以看到如果有非空字符串,你总是加 25,如果没有找到非空字符串,你总是减去 25。要解决此问题,您需要对名字、姓氏、电子邮件、密码中的每个文本更改进行跟踪。您可以将 HashMap 与 Enum 结合使用。像这样的东西:public YourActivity extends AppCompatActivity {&nbsp; &nbsp;...&nbsp; &nbsp;// this is the list of entry&nbsp; &nbsp;private enum Entry {&nbsp; &nbsp; &nbsp;FIRST_NAME, LAST_NAME, EMAIL, PASSWORD&nbsp; &nbsp;}&nbsp; &nbsp;// keep tracking the entry where Boolean is the value if entry already inserted&nbsp; &nbsp;private HashMap<Entry, Boolean> mMapEntries = new HashMap<>();&nbsp; &nbsp;private void checkAndValidateEntries() {&nbsp; &nbsp; &nbsp;// initialize the entry as not inserted yet&nbsp; &nbsp; &nbsp;mMapEntries.put(FIRST_NAME, false);&nbsp; &nbsp; &nbsp;mMapEntries.put(LAST_NAME, false);&nbsp; &nbsp; &nbsp;mMapEntries.put(EMAIL, false);&nbsp; &nbsp; &nbsp;mMapEntries.put(PASSWORD, false);&nbsp; &nbsp; &nbsp;// then handle the entry view&nbsp; &nbsp; &nbsp;// here the example only for firstname&nbsp; &nbsp; &nbsp;firstName.addTextChangedListener(new TextWatcher() {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onTextChanged(CharSequence s, int start, int before, int count) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int length = s.toString().trim().length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean isInserted = mMapEntries.get(FIRST_NAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// do changes if entry not yet inserted before.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(isInserted == false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // progress percentage per entry&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int percentage = 100 / mMapEntries.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the progress&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int progress = registrationProgress.getProgress() + percentage;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; registrationProgress.setProgress(progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we now have inserted the value, so mark it.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mMapEntries.put(FIRST_NAME, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean isInserted = mMapEntries.get(FIRST_NAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Do changes if entry already inserted before.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(isInserted == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int percentage = 100 / mMapEntries.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // subtract the progress.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int progress = registrationProgress.getProgress() - percentage;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;registrationProgress.setProgress(progress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// entry is removed, mark as not inserted yet&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mMapEntries.put(FIRST_NAME, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp;// handle the last name, e-mail, password&nbsp; &nbsp; &nbsp;...&nbsp; &nbsp;}}上面的代码只会在条目进度没有添加到之前的进度中时才会增加进度值,反之亦然。

皈依舞

问题是每次字段文本更改时都会添加 25。例如,如果你写“你好”,你有五个字母,所以onTextChanged会被触发 5 次。一个可能的工作区是为每个字段创建标志,仅boolean用于指示该字段是否已填充。然后您检查有多少标志设置为“已填充”并相应地设置ProgressBar。希望能帮助到你。
随时随地看视频慕课网APP

相关分类

Java
我要回答