1
先生,如果我更改爲其他活動,我如何保存在更改密碼活動中所做的更改。起初,我試圖只使用一個變量來保存值,但是如果我返回到其他活動,所做的所有更改將恢復正常。我試圖使用數組列表,但它是一樣的。你能教我其他創建更改密碼選項的方法嗎?感謝提前幫助。創建密碼選項 - android
public class ChangePassword extends Activity {
/** Called when the activity is first created. */
EditText enterPassword, enterNewPassword;
String defaultPassword = "1234";
ArrayList<String> newPasswordArray = new ArrayList<String>();
ArrayList<String> currentPasswordArray = new ArrayList<String>();
String newPassword;
String currentPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.changepassword);
Button save = (Button)findViewById(R.id.btnSaveSettings);
enterPassword = (EditText)findViewById(R.id.etEnterPassword);
enterNewPassword = (EditText)findViewById(R.id.etNewPassword);
currentPasswordArray.add("1234");
for(String s: newPasswordArray)
{
newPassword = s;
}
for(String s: currentPasswordArray)
{
currentPassword = s;
}
save.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
try
{
//get string from arraylist
for(String s: newPasswordArray)
{
newPassword = s;
}
for(String s: currentPasswordArray)
{
currentPassword = s;
}
//test password
if(enterPassword.getText().toString().equals(currentPassword))
{
if(enterNewPassword != null)
{
String newPass = setNewPassword(enterNewPassword.getText().toString());
setCurrentPassword(newPass);
Toast.makeText(getApplicationContext(), "Save Success", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Please fill up all fields.", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Current password, unmatched.", Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Save failed", Toast.LENGTH_LONG).show();
}
}
});
}//oncreate()
public String setNewPassword(String pass)
{
String returnPass = "";
newPasswordArray.clear();
newPasswordArray.add(pass);
for(String s : newPasswordArray)
{
returnPass = s;
}
return returnPass;
}//setNewPassword()
public String getPassword()
{
return newPassword;
}//getPassword
public void setCurrentPassword(String newpass)
{
currentPasswordArray.clear();
currentPasswordArray.add(newpass);
}//setCurrentPassword
}
我不明白你的問題。請重新格式化它。 –