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;
            }

}
4

1 回答 1

0

你确定你没有不匹配你的共享首选项文件名吗?
我可以看到你有一个myFolder = getSharedPreferences(filename, 0);where filename = "PasswordFile"。另一方面,您将密码保存到另一个名为passwd:的共享首选项文件getSharedPreferences("passwd", 0)中。

如果您没有特定需要使用多个共享首选项文件,我建议您使用应用程序的共享首选项的默认实例:PreferenceManager.getDefaultSharedPreferences(this)

于 2013-05-31T03:01:52.897 回答