您好我正在尝试为持久性/共享首选项编写代码。我成功地将数据保存在持久性存储中。但每次我启动我的应用程序时,它都会将我带到存储活动。我有两个活动主要活动和第二个活动。在主要活动中,我保存数据,第二个活动,我显示存储的数据。我想要的是第一次存储数据时它应该直接移动到第二个活动。我想实现像watsapp注册过程之类的东西,你第一次输入你的号码和别针,直到你卸载应用程序或手动清除数据它才会要求它。我该怎么办以下是我尝试过的代码。
public class MainActivity extends Activity {
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int id = settings.getInt("PREFS_NAME", 0);
if (id > 0) {
Intent second = new Intent(getApplicationContext(), seond.class);
startActivity(second);
}
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText email = (EditText) findViewById(R.id.editText2);
Button btn = (Button) findViewById(R.id.btnsaveprefs);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = settings.edit();
editor.putString("MSISDN", name.getText().toString());
editor.putString("PIN", email.getText().toString());
editor.commit();
Intent second = new Intent(getApplicationContext(), seond.class);
startActivity(second);
}
});
}
}
和我的第二项活动
public class seond extends Activity {
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tvname = (TextView) findViewById(R.id.uname);
TextView tvemail = (TextView) findViewById(R.id.uemail);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
tvname.setText(settings.getString("MSISDN", "Unknown"));
tvemail.setText(settings.getString("PIN", "Email default"));
}
}
答案 0 :(得分:0)
我认为你的问题在这里:
int id = settings.getInt("PREFS_NAME", 0);
您获取“PREFS_NAME”的值,然后检查它是否为> 0
,但您从未存储密钥PREFS_NAME
的值。我想你想在这里使用PIN
或MSISDN
?
答案 1 :(得分:0)
这是正确的方法:
public class MainActivity extends Activity {
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int id = settings.getInt("PREFS_NAME", 0);
if (id > 0) {
Intent second = new Intent(getApplicationContext(), Second.class);
startActivity(second);
}
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText email = (EditText) findViewById(R.id.editText2);
Button btn = (Button) findViewById(R.id.btnsaveprefs);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = settings.edit();
editor.putString("MSISDN", name.getText().toString());
editor.putString("PIN", email.getText().toString());
editors.putInt("PREFS_NAME", 1); // id = 1
editor.commit();
Intent second=new Intent(getApplicationContext(), Second.class);
startActivity(second);
}
});
}
}
public class Second extends Activity { // classes begin with CAPITAL
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tvname = (TextView) findViewById(R.id.uname);
TextView tvemail = (TextView) findViewById(R.id.uemail);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
tvname.setText(settings.getString("MSISDN", "Unknown"));
tvemail.setText(settings.getString("PIN", "Email default"));
}
}
请注意,您未在其他答案中设置ID。顺便提一下,你的编码风格很糟糕:永远不要用小写字母开始一个类名,为什么id
是一个int?你用它作为布尔值。为什么要在名为PIN
的首选项中保存一封电子邮件,并在名为MSISDN
的首选项中保存一个名称???