【发布时间】:2017-07-11 11:22:53
【问题描述】:
我在制作“记住我”复选框时遇到问题。我创建的代码只保存密码,不保存用户名。我正在将用户信息保存在数据库中。请帮助我在哪里做错了。这是我的代码;
public class LoginActivity extends AppCompatActivity {
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
private String username;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText userText = (EditText) findViewById(R.id.userText);
final EditText passText = (EditText) findViewById(R.id.passText);
final Button engButton = (Button) findViewById(R.id.engButton);
final Button loginButton = (Button) findViewById(R.id.loginButton);
final Button regButton = (Button) findViewById(R.id.regButton);
final Button exitButton = (Button) findViewById(R.id.exitButton);
final TextView loginText = (TextView) findViewById(R.id.loginText);
final TextView yaziText = (TextView) findViewById(R.id.yaziText);
final CheckBox beniBox = (CheckBox) findViewById(R.id.beniBox);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
userText.setText(loginPreferences.getString("username", ""));
passText.setText(loginPreferences.getString("password", ""));
beniBox.setChecked(true);
}
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user_name = userText.getText().toString();
final String password = passText.getText().toString();
if(view==loginButton){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(userText.getWindowToken(), 0);
if (beniBox.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", username);
loginPrefsEditor.putString("password", password);
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}
}
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 user_name = jsonResponse.getString("user_name");
Intent intent = new Intent(LoginActivity.this , ProfileActivity.class);
intent.putExtra("user_name", user_name);
LoginActivity.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(user_name, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent regIntent = new Intent(LoginActivity.this , RegisterActivity.class);
LoginActivity.this.startActivity(regIntent);
}
});
}
}
【问题讨论】:
-
问题类似,但这里的问题不一样。问题属于逻辑。这不应该被关闭
-
我无法发布答案,所以我将在这里写下您的问题。更改行 loginPrefsEditor.putString("username", username);使用 loginPrefsEditor.putString("username", user_name);变量用户名始终为空,在 onclick 方法中,您将用户名和密码设置为字段用户名和密码
-
@gmetax 非常感谢。这解决了我的问题。
-
您现在可以删除该帖子,因为它被错误地标记为重复