0
我想創建一個應用程序,該應用程序將在用戶下次使用此應用程序時保存密碼。我有我的If語句工作,但密碼不想保存。這是一個任務,我嘗試了一些東西,但似乎沒有任何工作。請幫忙!使用共享首選項設置並保存密碼
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class WheresMyPhoneActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button start;
private EditText pword;
private EditText confirm;
SharedPreferences myFolder;
public final String filename = "PasswordFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.btnStart);
pword = (EditText)findViewById(R.id.edtPassword);
confirm = (EditText)findViewById(R.id.edtConfirm);
myFolder = getSharedPreferences(filename, 0);
start.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String pass = pword.getText().toString();
String conf = confirm.getText().toString();
//Check that user typed in a password
if(pass.length()<6){
Toast.makeText(this, "Password must be at least 6 characters long", Toast.LENGTH_SHORT).show();
pword.setText("");
confirm.setText("");
pword.requestFocus();
return;
}
//Make sure two fields match
if(pass.equals(conf)){
//fields match, store configuration in shared preferences
Editor passwdfile = getSharedPreferences("passwd", 0).edit();
passwdfile.putString("passwd",pass);
passwdfile.commit();
finish();
startActivity(new Intent(WheresMyPhoneActivity.this, Home.class));
}else{//password mismatch - start over
Toast.makeText(this, "Passwords must match", Toast.LENGTH_SHORT).show();
pword.setText("");
confirm.setText("");
pword.requestFocus();
return;
}
}
'...但密碼不對想要保存.'。那麼它說什麼?你怎麼知道它不能保存? –