gpt4 book ai didi

android - 如何在android中的共享首选项中存储多个值?

转载 作者:行者123 更新时间:2023-11-30 02:57:23 24 4
gpt4 key购买 nike

文件名 = MainActivity.java

公共(public)类 MainActivity 扩展 Activity {

TextView textView1;
EditText editText1,editText2;
Button button1,button2;
public static final String Default = "NA";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)findViewById(R.id.textViewSignup);
textView1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Sign_up.class);
startActivity(i);
}
});
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
button1 =(Button)findViewById(R.id.button1);
button2 =(Button)findViewById(R.id.button2);


button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SharedPreferences preferences = getSharedPreferences("Login Credentials",MODE_PRIVATE);

String firstname=preferences.getString("Name",Default );
String email= preferences.getString("Email",Default );
String password=preferences.getString("Password", Default);




if(editText1.getText().toString().equals(firstname)&&
editText2.getText().toString().equals(password)){

Toast.makeText(getApplicationContext(), "Welcome User", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(getApplicationContext(), "Wrong User", Toast.LENGTH_SHORT).show();

}
}
});
button2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});



}

第二个文件名 = Signup.java

公共(public)类 Sign_up 扩展 MainActivity 实现 OnClickListener{

   public static final String Default = "MyPrefs" ;


String name,email,password,confirmpassword;
EditText et1,et2,et3,et4;
TextView textView;
Button button;
SharedPreferences preferences;
Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);

et1 = (EditText)findViewById(R.id.editTextName);
et2 = (EditText)findViewById(R.id.editEmail);
et3 = (EditText)findViewById(R.id.editPassword);
et4 = (EditText)findViewById(R.id.editConPassword);
button = (Button)findViewById(R.id.buttonSignup);
textView = (TextView)findViewById(R.id.textViewLoginhere);
button.setOnClickListener(this);


textView.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Sign_up.this,MainActivity.class);
startActivity(intent);
}
});


}
//public void sharedPrefarence(){


//}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
preferences = getSharedPreferences("Login Credentials", MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
//editor = preferences.edit();
editor.putString("Name", et1.getText().toString());
editor.putString("Email",et2.getText().toString());
editor.putString("Password", et3.getText().toString());
editor.putString("Confirmpassword", et4.getText().toString());
editor.commit();

Toast.makeText(getApplicationContext(), "Added Successful",
Toast.LENGTH_SHORT).show();
}

此代码有效,但当我添加新用户时, key 被覆盖,这就是为什么它无法存储多个值,所以请给我正确的建议

感谢和问候拉克什

最佳答案

共享首选项仅存储简单的名称/值对。它们的键名是唯一的,因此当您为“姓名”、“电子邮件”、“密码”和“确认密码”输入值时,它总是会覆盖原来的内容。

看起来您需要多组这些字段,因此您可能希望将数据存储在 sqllite 数据库中。

关于android - 如何在android中的共享首选项中存储多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23053068/

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