3 回答

TA貢獻1878條經驗 獲得超4個贊
當我運行上面的程序時,我會看到進度條繼續在每個文本條目中添加 25 [...] 我做錯了什么?
TextWatcher 的 onTextChanged、beforeTextChanged 和 afterTextChanged 之間的差異將幫助您了解為什么您的應用會出現這種行為。
我該怎么辦?
您可以覆蓋afterTextChanged()而不是onTextChanged()因為您對用戶鍵入字母(或按退格鍵)的頻率不感興趣。在 中afterTextChanged(),您評估到目前為止String已輸入的 的長度EditText。
為了跟蹤進度,我想介紹一個
private SparseBooleanArray isTextComplete = new SparseBooleanArray();
以及一種方法
private void checkProgress(){
int progress = 0;
for(int i = 0; i < isTextComplete.size(); i++){
if(isTextComplete.valueAt(i)){
progress++;
}
}
registrationProgress.setProgress(progress * 25);
}
在 中afterTextChanged(),您可以更新SparseBooleanArray并調用checkProgress():
@Override
public void afterTextChanged(Editable s) {
int len = etMail.getText().toString().length();
if(len > 0){
isTextComplete.put(etMail.getId(), true);
}
else{
isTextComplete.put(etMail.getId(), false);
}
checkProgress();
}

TA貢獻1890條經驗 獲得超9個贊
當我運行上面的時候,我得到了進度條,繼續為每個文本添加 25
這是因為您正在使用以下代碼(請閱讀評論):
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = s.toString().trim().length();
// if string not empty, keep adding the progress with 25
if (length > 0) {
registrationProgress.setProgress(registrationProgress.getProgress() + 25);
}
// subtract 25 each time string empty.
else {
registrationProgress.setProgress(registrationProgress.getProgress() - 25);
}
}
你可以看到如果有非空字符串,你總是加 25,如果沒有找到非空字符串,你總是減去 25。
要解決此問題,您需要對名字、姓氏、電子郵件、密碼中的每個文本更改進行跟蹤。您可以將 HashMap 與 Enum 結合使用。像這樣的東西:
public YourActivity extends AppCompatActivity {
...
// this is the list of entry
private enum Entry {
FIRST_NAME, LAST_NAME, EMAIL, PASSWORD
}
// keep tracking the entry where Boolean is the value if entry already inserted
private HashMap<Entry, Boolean> mMapEntries = new HashMap<>();
private void checkAndValidateEntries() {
// initialize the entry as not inserted yet
mMapEntries.put(FIRST_NAME, false);
mMapEntries.put(LAST_NAME, false);
mMapEntries.put(EMAIL, false);
mMapEntries.put(PASSWORD, false);
// then handle the entry view
// here the example only for firstname
firstName.addTextChangedListener(new TextWatcher() {
...
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = s.toString().trim().length();
if (length > 0) {
boolean isInserted = mMapEntries.get(FIRST_NAME);
// do changes if entry not yet inserted before.
if(isInserted == false) {
// progress percentage per entry
int percentage = 100 / mMapEntries.size();
// add the progress
int progress = registrationProgress.getProgress() + percentage;
registrationProgress.setProgress(progress);
// we now have inserted the value, so mark it.
mMapEntries.put(FIRST_NAME, true);
}
} else {
boolean isInserted = mMapEntries.get(FIRST_NAME);
// Do changes if entry already inserted before.
if(isInserted == true) {
int percentage = 100 / mMapEntries.size();
// subtract the progress.
int progress = registrationProgress.getProgress() - percentage;
registrationProgress.setProgress(progress);
// entry is removed, mark as not inserted yet
mMapEntries.put(FIRST_NAME, false);
}
}
}
...
});
// handle the last name, e-mail, password
...
}
}
上面的代碼只會在條目進度沒有添加到之前的進度中時才會增加進度值,反之亦然。

TA貢獻1851條經驗 獲得超3個贊
問題是每次字段文本更改時都會添加 25。例如,如果你寫“你好”,你有五個字母,所以onTextChanged
會被觸發 5 次。
一個可能的工作區是為每個字段創建標志,僅boolean
用于指示該字段是否已填充。然后您檢查有多少標志設置為“已填充”并相應地設置ProgressBar
。
希望能幫助到你。
添加回答
舉報