gpt4 book ai didi

java - 如何使用 sharedPreference 类存储密码?

转载 作者:行者123 更新时间:2023-11-30 03:44:19 26 4
gpt4 key购买 nike

我正在开发一个使用消息获取数据的安卓应用程序。用于创建密码的代码是我的代码,我如何使用 sharedPreference 类存储密码?

     public class MainActivity extends Activity {

Button Switchon;
EditText passwd; //button name
String ms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

passwd = (EditText) findViewById(R.id.passwd);
Switchon = (Button) findViewById(R.id.Switchon);



Switchon.setOnClickListener(new View.OnClickListener() {



@Override
public void onClick(View arg0) {
@SuppressWarnings("unused")
String ms = passwd.getText().toString();
Toast.makeText(getApplicationContext(), "You have successfully created and this app is on", Toast.LENGTH_SHORT).show();

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}



}

最佳答案

在共享首选项中存储值:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("password","123456");
editor.commit();

从共享首选项中检索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("password","");

关于java - 如何使用 sharedPreference 类存储密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245050/

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