[英]using sharedpreferences for password - android
先生,如何在其他類中訪問我的sharedpreferences文件? 我希望應用程序的默認密碼為1234,然后根據用戶的需要進行更改。 我已經使用過這段代碼並且它第一次運行,但是當我重新啟動應用程序時,它將恢復為舊密碼。 提前感謝您的幫助
public class ChangePassword extends Activity {
/** Called when the activity is first created. */
EditText enterPassword, newPassword;
public final String filename = "PasswordFile";
String myPassword;
SharedPreferences myFolder;
public static String dataReturned = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.changepassword);
Button save = (Button)findViewById(R.id.btnSavePassword);
enterPassword = (EditText)findViewById(R.id.etCurrentPassword);
newPassword = (EditText)findViewById(R.id.etNewPassword);
myFolder = getSharedPreferences(filename, 0);//a folder
dataReturned = myFolder.getString("passwordKey", "");
if(dataReturned.equals(""))
{
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", "1234"); //newData is new pass, passwordKey is key
editor.commit();
}
save.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
String stringData = enterPassword.getText().toString().trim();
dataReturned = myFolder.getString("passwordKey", ""); //key/def message
//if stored password is equal to entered password
if(dataReturned.equals(stringData))
{
String newData = newPassword.getText().toString().trim();
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", newData); //newData is new pass, passwordKey is key
editor.commit();
dataReturned = myFolder.getString("passwordKey", "couldn't load data"); //get file from sharedpref
Toast.makeText(getApplicationContext(),
"Password successfully changed",
Toast.LENGTH_LONG).show();
enterPassword.setText("");
newPassword.setText("");
}
else
{
Toast.makeText(getApplicationContext(),
"Wrong password!",
Toast.LENGTH_LONG).show();
enterPassword.setText("");
newPassword.setText("");
}
}
});
}//oncreate()
}
這里是我想在我的其他課程中使用共享偏好中保存的數據的地方
public void dispatch_button_action(View view){
final EditText password_input = new EditText(this); // create an text input field
password_input.setHint("Enter Password"); // put a hint in it
password_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // change it to password type
AlertDialog.Builder alertDialog = new Builder(this); // create an alert box
alertDialog.setTitle("Enter Password"); // set the title
alertDialog.setView(password_input); // insert the password text field in the alert box
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button
public void onClick(DialogInterface dialog, int which) {
String entered_password = password_input.getText().toString();
String password = ChangePassword.myFolder.getString("passwordKey", "");
if(!password.trim().equals(""))
{
testPassAndSendSms(password.trim(), entered_password);
}
else
{
testPassAndSendSms(my_password, entered_password);
}
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show(); // show the alert box
}//end dispatch_button
目前您沒有保存用戶在Alertbox中輸入的最新密碼,因此在Another Activity中將SharedPreferences中的最新密碼保存為:
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String entered_password = password_input.getText().toString();
SharedPreferences myFolder = You_Current_Activitu.this.getSharedPreferences(filename, 0);
String dataReturned = myFolder.getString("passwordKey", "");
if(dataReturned.equals(""))
{
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", entered_password);
editor.commit();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.