gpt4 book ai didi

android - 注销清除 SharedPreferences

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:32 26 4
gpt4 key购买 nike

我有一个将用户名密码 保存到SharedPreferences 的登录页面。我有另一个包含注销按钮的 Activity 类。我想在单击注销按钮时清除 SharedPreferences。问题是我没有从此类中获取 SharedPreferences。如何获取 SharedPreferences

登录页面

public class MainActivity extends Activity {
public SharedPreferences.Editor loginPrefsEditor;
public SharedPreferences loginPreferences;
private Boolean saveLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name = (EditText) findViewById(R.id.et_Username);
pass = (EditText) findViewById(R.id.et_Password);
login = (Button) findViewById(R.id.bt_Login);

loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();

saveLogin = loginPreferences.getBoolean("saveLogin", false);

if (saveLogin == true) {
name.setText(loginPreferences.getString("username", ""));
pass.setText(loginPreferences.getString("password", ""));
}

login.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
name1 = name.getText().toString();
pass1 = pass.getText().toString();
//new Thread (new Task()).start();
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", name1);
loginPrefsEditor.putString("password", pass1);
loginPrefsEditor.commit();
new myAsyncTask().execute();
}
});
}

AnotherActivity 中的注销按钮

 logout.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
// Launching News Feed Screen

Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});

最佳答案

试试这个!

logout.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
// Launching News Feed Screen

SharedPreferences preferences =getSharedPreferences("loginPrefs",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
finish();
});

关于android - 注销清除 SharedPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163708/

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