我做了一个简单的登录活动,其中我使用android sqlite数据库来存储登录信息,即用户名和密码。当用户在注册活动中单击创建帐户按钮时,他/她将被转移到另一个活动,在那里他们可以看到他们的用户名和通行证。在该屏幕中,我需要检查首选项是否清晰,然后根据决定更改按钮的文本。
我使用SharedPreference
传递了活动之间的信息,但是当用户点击退出时我无法将其删除或清除..以下是这两项活动的代码..
这是我的SignUp.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get Refferences of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
tv=(TextView)findViewById(R.id.textView);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")) {
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword)) {
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
if(password.length()<8) {
Toast.makeText(getApplicationContext(),"Password must be 8 digits longer",Toast.LENGTH_LONG).show();
}
else {
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password);
try {
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Name,userName);
editor.putString(Pass,password);
editor.commit();
} catch (NullPointerException e) {
e.printStackTrace();
}
intent=new Intent(getApplicationContext(),AfterLogin.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
loginDataBaseAdapter.close();
}
AfterLogin.Java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.afterlogin);
username=(TextView)findViewById(R.id.Username);
name=(TextView)findViewById(R.id.Name);
user=(TextView)findViewById(R.id.User);
pass=(TextView)findViewById(R.id.Pass);
sout=(Button)findViewById(R.id.SignOut);
s= sharedPreferences.getString(Name,"");
us=sharedPreferences.getString(Pass,"");
username.setText(s);
name.setText(s);
user.setText(s);
pass.setText(us);
sout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.clear();
editor.commit();
String n=sharedPreferences.getString(Name,"");
if (n == null) {
sout.setText("Signed Out");
}
}
});
}
答案 0 :(得分:1)
AfterLogin.java你已经检查了
n == null
但是为String n
传递了默认值为空String n = sharedPreferences.getString(Name,“”);的 // EMPTY 强>
变化
if (n==null) {
sout.setText("Signed Out");
}
到
if (n.isEmpty()) {
sout.setText("Signed Out");
}
答案 1 :(得分:0)
你能尝试提交clear并在一行中提交。像editor.clear()。commit()或editor.clear()。apply()。这可能对您有所帮助,因为您在进行更改后在编辑器对象上提交或应用更改。
答案 2 :(得分:0)
问题可能是您使用的String n=sharedPreferences.getString(Name,"");
总是返回一些内容。如果首选项不存在则为默认值,如果存在则为默认值。
要检查首选项是否已被删除,我最好使用String n=sharedPreferences.contains(Name);
,它返回一个布尔值,显示首选项是否存在。
希望它有所帮助!