gpt4 book ai didi

android - 无法清除共享首选项

转载 作者:行者123 更新时间:2023-11-30 00:50:01 25 4
gpt4 key购买 nike

我做了一个简单的登录 Activity ,其中我使用 android sqlite 数据库来存储登录信息,即用户名和密码。当用户在注册 Activity 中单击创建帐户按钮时,他/她将被转移到另一个 Activity ,在那里他们可以看到他们的用户名和密码。在该屏幕中,我需要检查首选项是否明确,然后按钮的文本将根据决定进行更改。

我使用 SharedPreference 在 Activity 之间传递了信息,但是当用户单击注销时我无法删除或清除它。下面是两个 Activity 的代码。

这是我的 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");
}
}
});
}

最佳答案

你检查过的AfterLogin.java

n == null

但将字符串 n 的默认值传递为空

String n=sharedPreferences.getString(Name,""); //EMPTY

改变

if (n==null) {
sout.setText("Signed Out");
}

if (n.isEmpty()) {
sout.setText("Signed Out");
}

关于android - 无法清除共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com