我编写了共享的首选项代码,它对我不起作用,问题是什么?
我编写了共享的首选项代码,它对我不起作用,我希望当名称为学术和密码为123时,我希望它移至特定的活动,我希望当名称是入学和入学时,我希望它移至另一个活动密码是456。问题在哪里?
//这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = findViewById(R.id.btnLogin);
name = findViewById(R.id.inputUsername);
password = findViewById(R.id.inputPassword);
checkBox = findViewById(R.id.checkBox);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences preferences = getSharedPreferences("login data", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if (checkBox.isChecked()) {
editor.putString("username", name.getText().toString());
editor.putString("MyPassword", password.getText().toString());
editor.putBoolean("checked", true);
editor.apply();
} else {
editor.remove("username");
editor.remove("MyPassword");
editor.remove("checked");
editor.apply();
}
if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
Toast.makeText(LoginActivity.this, "empty data", Toast.LENGTH_SHORT).show();
}
if (name.getText().toString().equals(true_user_name_Admission) && password.getText().toString().equals(true_password_Admission)) {
Intent l = new Intent(LoginActivity.this, AdmissionDashboardActivity.class);
startActivity(l);
Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
} else {
if (name.getText().toString().equals(true_user_name_Academic) && password.getText().toString().equals(true_password_Academic)) {
Intent ad = new Intent(LoginActivity.this, AcademicDashboardActivity.class);
startActivity(ad);
Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "Required", Toast.LENGTH_SHORT).show();
name.setError("Required");
password.setError("Required");
}
}
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sp = getSharedPreferences("login data", MODE_PRIVATE);
name.setText(sp.getString("username", ""));
password.setText(sp.getString("MyPassword", ""));
checkBox.setChecked(sp.getBoolean("checked", false));
}
}
I wrote the shared preferences code and it did not work for me, I want it to move to a specific activity when the name is academic and the password is 123, and I want it to move to another activity when the name is admission and the password is 456. Where is the problem?
//this is my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = findViewById(R.id.btnLogin);
name = findViewById(R.id.inputUsername);
password = findViewById(R.id.inputPassword);
checkBox = findViewById(R.id.checkBox);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences preferences = getSharedPreferences("login data", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if (checkBox.isChecked()) {
editor.putString("username", name.getText().toString());
editor.putString("MyPassword", password.getText().toString());
editor.putBoolean("checked", true);
editor.apply();
} else {
editor.remove("username");
editor.remove("MyPassword");
editor.remove("checked");
editor.apply();
}
if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
Toast.makeText(LoginActivity.this, "empty data", Toast.LENGTH_SHORT).show();
}
if (name.getText().toString().equals(true_user_name_Admission) && password.getText().toString().equals(true_password_Admission)) {
Intent l = new Intent(LoginActivity.this, AdmissionDashboardActivity.class);
startActivity(l);
Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
} else {
if (name.getText().toString().equals(true_user_name_Academic) && password.getText().toString().equals(true_password_Academic)) {
Intent ad = new Intent(LoginActivity.this, AcademicDashboardActivity.class);
startActivity(ad);
Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "Required", Toast.LENGTH_SHORT).show();
name.setError("Required");
password.setError("Required");
}
}
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sp = getSharedPreferences("login data", MODE_PRIVATE);
name.setText(sp.getString("username", ""));
password.setText(sp.getString("MyPassword", ""));
checkBox.setChecked(sp.getBoolean("checked", false));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能尝试这个吗
can you try this