3 回答

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
當(dāng)我運(yùn)行上面的程序時(shí),我會(huì)看到進(jìn)度條繼續(xù)在每個(gè)文本條目中添加 25 [...] 我做錯(cuò)了什么?
TextWatcher 的 onTextChanged、beforeTextChanged 和 afterTextChanged 之間的差異將幫助您了解為什么您的應(yīng)用會(huì)出現(xiàn)這種行為。
我該怎么辦?
您可以覆蓋afterTextChanged()而不是onTextChanged()因?yàn)槟鷮?duì)用戶鍵入字母(或按退格鍵)的頻率不感興趣。在 中afterTextChanged(),您評(píng)估到目前為止String已輸入的 的長度EditText。
為了跟蹤進(jìn)度,我想介紹一個(gè)
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并調(diào)用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貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
當(dāng)我運(yùn)行上面的時(shí)候,我得到了進(jìn)度條,繼續(xù)為每個(gè)文本添加 25
這是因?yàn)槟谑褂靡韵麓a(請(qǐng)閱讀評(píng)論):
@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。
要解決此問題,您需要對(duì)名字、姓氏、電子郵件、密碼中的每個(gè)文本更改進(jìn)行跟蹤。您可以將 HashMap 與 Enum 結(jié)合使用。像這樣的東西:
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
...
}
}
上面的代碼只會(huì)在條目進(jìn)度沒有添加到之前的進(jìn)度中時(shí)才會(huì)增加進(jìn)度值,反之亦然。

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