将 EditText 中的值存储在设备内存中,并从 EditText 中的内存中恢复
Store values from EditText in device's memory and restore back from memory in EditText
我有以下登录 Activity,在成功给出登录值后跳转到我的 MainMenuActivity。
我需要在 Intent 之前将我的 2 个 EdiText 字段的值存储在设备的内存中,并在下次我 运行 我的应用程序 (LoginActivity).[=11= 时将其恢复回 EditText 字段]
@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;
}
}
}
});
}
这怎么可能?
在共享首选项中保存您的用户名和密码
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.
我有以下登录 Activity,在成功给出登录值后跳转到我的 MainMenuActivity。 我需要在 Intent 之前将我的 2 个 EdiText 字段的值存储在设备的内存中,并在下次我 运行 我的应用程序 (LoginActivity).[=11= 时将其恢复回 EditText 字段]
@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;
}
}
}
});
}
这怎么可能?
在共享首选项中保存您的用户名和密码
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.