将EditText中的值存储在设备的内存中,并从EditText中的内存中恢复

时间:2017-05-22 01:25:22

标签: android login android-edittext android-memory

我有以下登录活动,在成功登录后会跳转到我的MainMenuActivity。 我需要在Intent之前将我的2个EdiText字段中的值存储在设备的内存中,并在下次运行我的应用程序时将其恢复到EditText字段(LoginActivity)。

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    md = MediaPlayer.create(LoginActivity.this, R.raw.tick);

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);
    final Button bLogin = (Button) findViewById(R.id.bSignIn);



    tvRegisterLink.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(lock==1) {

                lock = 0;
                md.release();
                md = MediaPlayer.create(LoginActivity.this, R.raw.tick);
                md.start();

                Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                LoginActivity.this.startActivity(registerIntent);
                finish();

            }
        }
    });

    bLogin.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        public void onClick(View v) {

            if (lock == 1) {

                connected=isConnectedToNet();

                if(connected) {

                    lock = 0;
                    final String username = etUsername.getText().toString();
                    final String password = etPassword.getText().toString();

                    md.release();
                    md = MediaPlayer.create(LoginActivity.this, R.raw.tick);
                    md.start();

                    /***Send username and password to server.Take back user's parameters ***/

                    Response.Listener<String> responseListener = new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            try {

                                JSONObject jsonResponse = new JSONObject(response);
                                boolean success = jsonResponse.getBoolean("success");

                                if (success) {

                                    String name = jsonResponse.getString("name");
                                    int age = jsonResponse.getInt("age");
                                    int score = jsonResponse.getInt("score");
                                    int level = jsonResponse.getInt("level");
                                    int online = jsonResponse.getInt("online");

                                    Intent intent = new Intent(LoginActivity.this, MainMenuActivity.class);
                                    intent.putExtra("username", username);
                                    intent.putExtra("score", score);
                                    intent.putExtra("level", level);
                                    intent.putExtra("online", online);
                                    LoginActivity.this.startActivity(intent);
                                    finish();

                                } else {

                                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this, R.style.myBackgroundStyle);
                                    builder.setMessage("Λάθος όνομα χρήστη ή κωδικός")
                                            .setNegativeButton("Ξαναπροσπάθησε", null)
                                            .create()
                                            .show();

                                    /***Set lock back to value '1' .***/

                                    Runnable runnable = new Runnable() {
                                        @Override
                                        public void run() {

                                            lock = 1;

                                        }

                                    };
                                    mHandler.postDelayed(runnable, 1000);
                                }

                            } catch (JSONException e) {

                                e.printStackTrace();

                            }
                        }
                    };

                    Response.ErrorListener error=new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                            recursive();

                        }
                    };
                    LoginRequest loginRequest = new LoginRequest(username, password, responseListener,error);
                    //RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                    //queue.add(loginRequest);
                    Mysingleton.getmInstance(getApplicationContext()).addToRequestque(loginRequest);

                }

                else {

                    Snackbar snackbar = Snackbar.make(findViewById(R.id.bSignIn), "Δεν υπάρχει σύνδεση στο διαδίκτυο", Snackbar.LENGTH_LONG);
                    View sbView = snackbar.getView();

                    TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                    textView.setGravity(Gravity.CENTER);
                    sbView.setBackgroundResource(R.drawable.snackbar1);
                    textView.setTextColor(Color.BLACK);
                    textView.setTypeface(null, Typeface.BOLD_ITALIC);
                    textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.drawable.image3,0);

                    snackbar.show();

                    lock=1;

                }
            }
        }
    });
}

怎么可能这样做?

1 个答案:

答案 0 :(得分:1)

在共享偏好设置中保存您的用户名和密码

SharedPreferences.Editor editor = getSharedPreferences("LOGIN_DETAILS", MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("password", "123456");
editor.commit();

随时接收值

 SharedPreferences prefs = getSharedPreferences("LOGIN_DETAILS", MODE_PRIVATE); 

  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  String password = prefs.getString("password","0"); //0 is the default value.