我製作了一個應用程序,該應用程序以用戶輸入密碼的屏幕開始。我在底部有文字0123',並且希望每次密碼不正確時它都會從3減少到2(我已經有一個IF else語句,如果密碼是正確的,可以添加此方法的其他),但我不知道如何更新字符串?在運行應用程序時更改字符串
我在考慮使用For循環,並嘗試這樣的:
public String textAttempts(View view){
String attemptsRemaining = getString(R.string.attempts_Remaining);
int loopVal;
for(loopVal=1;loopVal>=0;loopVal--){
String attmeptsRemaining =
attemptsRemaining.replace(loopVal, loopVal);
}
return attemptsRemaining;
}
但是它變得非常混亂,由於不必INT轉換爲字符串,並且也使得REPLACE語句不滿。 有沒有人有更好的方式做到這一點?
字符串的XML文件:
<resources>
<string name="app_name">Stock Control System FINAL</string>
<string name="action_settings">Settings</string>
<string name="enter_password">Enter Password</string>
<string name="edit_message">Enter a message</string>
<string name="button_confirm">Confirm</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_main_menu_passed">MainMenuPassed</string>
<string name="button_stock">Stock</string>
<string name="button_products">Products</string>
<string name="button_supplies">Incoming Supplies</string>
<string name="button_suppliers">Suppliers</string>
<string name="button_settings">Settings</string>
<string name="title_activity_main_stock">MainStock</string>
<string name="title_activity_main_menu">MainMenu</string>
<plurals name="attempts_remaining">
<item quantity="one">You have one attempt remaining.</item>
<item quantity="other">You have %d attempts remaining.</item>
</plurals>
</resources>
您還在創建一個新的字符串,並且永遠不會返回它。 'attmeptsRemaining'和'attemptsRemaining'不一樣# – Doomsknight