Hej
Jag använder mig just nu av en loginsida i min app. det fungerar bra att logga in/registera sig etc.
Nu skulle jag vilja implementera en checkbox för automatisk inloggning.
Den metoden jag vill använda mig av är sharedpreferences.
Kod:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.login);
final EditText usernameTextBox = (EditText) findViewById(R.id.txt_username);
final EditText passwordTextBox = (EditText) findViewById(R.id.txt_password);
final CheckBox autoLogin = (CheckBox) findViewById(R.id.AutoLogin);
TextView regText = (TextView) findViewById(R.id.register);
SharedPreferences preferences = getSharedPreferences("company", 0);
usernameTextBox.setText(preferences.getString("username", null));
passwordTextBox.setText(preferences.getString("password", null));
if (sUserName == null || sPassword == null) {
//Prompt for username and password
// this is the action listener
loginButton.setOnClickListener( new OnClickListener() {
public void onClick(View viewParam) {
// this gets the resources in the xml file and assigns it to a local variable of type EditText
if (autoLogin.isChecked()) {
SharedPreferences preferences = getSharedPreferences("company", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username", sUserName);
editor.putString("password", sPassword);
editor.commit();
}