1

先生,如果我更改为另一个活动,我如何保存在更改密码活动中所做的更改。起初,我尝试只使用变量来保存值,但如果我返回其他活动,所做的所有更改都会恢复正常。我试过使用arraylist,但它是一样的。你能教我其他创建更改密码选项的方法吗?提前感谢您的帮助。

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

}

4

1 回答 1

1

您必须使用 Android 平台提供的 3 种方式之一来保存您的数据:

  1. 共享首选项
  2. SQLite数据库
  3. 文件

如果您只想保存并再次恢复。

但是,如果您只想保存活动的实例,那么您可以覆盖适当的方法。

于 2012-12-05T23:38:53.267 回答