我有一個Activity
,我想在其中輸入PIN碼。在這個Activity
用戶可以去單獨的Activity
,他可以在其中設置PIN。如果未設置PIN,則應使用默認PIN碼。Android使用Shared Preference作爲密碼
這是第一個Activity
的代碼:
public class activity_identification extends AppCompatActivity {
SharedPreferences pins_pref = null;
EditText pin_entry = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_identification);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
pin_eingabe = (EditText) findViewById(R.id.et_pin_entry);
pins_pref = getApplicationContext().getSharedPreferences("UserPin", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = pins_pref.edit();
edit.putString("default_pin", "0000");
edit.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pin_identifikation, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_change_pin) {
startActivity(new Intent(this, pin_settings.class));
return true;
}
else if (id == R.id.home) {
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickPinSummit(View button){
if(pin_entry.getText().toString().equals(pins_pref.getString("user_pin", null).toString())) {
Toast.makeText(this, "Authorization Right", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Authorization Wrong", Toast.LENGTH_SHORT).show();
}
}
}
這是第二Activity
:
public class pin_settings extends AppCompatActivity {
EditText et_old_pin = null;
EditText et_new_pin = null;
EditText et_new_pin_repeat = null;
Button btn_pin_save = null;
TextView tv_sharedPIN = (TextView) null;
SharedPreferences pins_prefs = null;
private String default_pin = null;
private String user_pin = null;
private String old_pin = null;
private String new_pin = null;
private String new_pin_repeat = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pin_einstellungen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
et_old_pin = (EditText) findViewById(R.id.et_old_pin);
et_new_pin = (EditText) findViewById(R.id.et_new_pin);
et_new_pin_repeat = (EditText) findViewById(R.id.et_new_pin_repeat);
btn_pin_save = (Button) findViewById(R.id.btn_pin_save);
tv_sharedPIN = (TextView) findViewById(R.id.sharedPIN);
pins_prefs = getApplicationContext().getSharedPreferences("UserPin", Context.MODE_PRIVATE);
general_pin = pins_prefs.getString("default_pin", null);
tv_sharedPIN.setText(pins_prefs.getString("general_pin", null));
}
public void onClickNewPinSave(View button){
old_pin = et_old_pin.getText().toString();
new_pin = et_new_pin.getText().toString();
new_pin_repeat = et_new_pin_repeat.getText().toString();
if(old_pin.equals(pins_prefs.getString("default_pin",null).toString())){
if (new_pin.equals(new_pin_repeat)){
SharedPreferences.Editor edit = pins_prefs.edit();
edit.putString("user_pin", user_pin);
edit.commit();
tv_sharedPIN.setText(pins_prefs.getString("user_pin", null));
} else {
Toast.makeText(this, "New Pins are not the same", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Old Pin is wrong.", Toast.LENGTH_LONG).show();
}
}
}
現在的問題是,應用程序不保存這是在設定的PIN第二個Activity
。只要應用程序處於打開狀態,它就會工作,但只要重新啓動,PIN碼就會恢復爲默認PIN碼。我認爲這是因爲我在第一個Activity
的onCreate()
方法中設置了默認PIN碼,但我無法弄清楚如何解決這個問題。
「Glabal」是什麼意思分享了prefrences? – JIGAR
您可以從任何獲取上下文的地方訪問'SharedPreferences'。 –
你是什麼意思的上下文?我認爲我不太瞭解共享偏好功能原理.. –