我一直在尝试在登录屏幕中添加“记住我”功能。
但是以某种方式不能保存我的登录凭据吗?

有人可以告诉我我在做什么错吗?

我还在学习,所以如果有人有一些建议可以继续前进,让我开心:D

这是我的代码

public class WvvZondag extends Activity {

    EditText un,pw;
    TextView error;
    Button ok, cancel;
    CheckBox checkbox;
    private String username, password;
    private SharedPreferences loginPreferences;
    private SharedPreferences.Editor loginPrefsEditor;
    private Boolean saveLogin;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy =
                new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }


        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_wvv_zondag);
        un=(EditText)findViewById(R.id.username);
        pw=(EditText)findViewById(R.id.password);
        ok=(Button)findViewById(R.id.login_button);
        cancel=(Button)findViewById(R.id.cancel_button);
        error=(TextView)findViewById(R.id.error);
        checkbox=(CheckBox)findViewById(R.id.checkBox1);
        loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
        loginPrefsEditor = loginPreferences.edit();

        saveLogin = loginPreferences.getBoolean("saveLogin", false);
        if (saveLogin == true){
            un.setText(loginPreferences.getString("un", ""));
            pw.setText(loginPreferences.getString("pw", ""));
            checkbox.setChecked(true);
        }

        cancel.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                finish();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                username = un.getText().toString();
                password = pw.getText().toString();

                if (checkbox.isChecked()) {
                    loginPrefsEditor.putBoolean("saveLogin", true);
                    loginPrefsEditor.putString("username", username);
                    loginPrefsEditor.putString("password", password);
                    loginPrefsEditor.commit();

                    // TODO Auto-generated method stub
                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                    postParameters.add(new BasicNameValuePair("username", username));
                    postParameters.add(new BasicNameValuePair("password", password));

                String response = null;

                try {
                    response = CustomHttpClient.executeHttpPost("http://www.wvvzondag2.nl/android/check.php", postParameters);

                    String res=response.toString();

                    res = res.replaceAll("\\s+", "");

                    if(res.equals("1")){
                        Intent loginintent = new Intent(WvvZondag.this, Menu_show.class);
                        startActivity(loginintent);
                    }
                    else
                        error.setText("Gebruikersnaam en/of wachtwoord is incorrect");

                } catch (Exception e) {
                    un.setText(e.toString());
                }
                } else {
                    loginPrefsEditor.clear();
                    loginPrefsEditor.commit();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_wvv_zondag, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case R.id.menu_website:
            startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.wvvzondag2.nl")));
            return true;
        }

        return false;
    }
}

最佳答案

您不应该使用它来检索信息吗?

    un.setText(loginPreferences.getString("username", ""));
    pw.setText(loginPreferences.getString("password", ""));

08-05 18:02