共享首选项未保存或循环不起作用
Shared preference not saved or loop does'nt work
如有帮助,将不胜感激。我有两个活动。 MainActivity 和 SharedPrefs。我希望打开该应用程序以查看是否有一些已保存的首选项。如果不是,它必须转到另一个 activity 那里询问一些细节。然后在按下提交按钮时,它必须保存共享首选项并转到 MainActivity。但它没有,它只是跳回到 SharedPrefs activity。现在我不知道我的共享首选项是否没有被保存(我认为 "editor.commit" 会完成这项工作),或者我的循环有问题。我确实改变了我的循环,它正在以相反的方式工作,但我可能会把整个事情弄错,因为我对 Android 和 Java 很陌生。就像我说的,一些帮助将不胜感激。
这是我的 MainActivity 和我的循环:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
boolean check = sharedPreferences.getBoolean("Check",false);
if(!check){
//Intent intent;
Intent SharedPrefsIntent = new Intent(MainActivity.this, SharedPrefs.class);
startActivity(SharedPrefsIntent); }
else {
setContentView(R.layout.activity_main);
TextView brands = (TextView) findViewById(R.id.brands);
brands.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent brandsIntent = new Intent(MainActivity.this, brands.class);
startActivity(brandsIntent);
}
});
}
}
这是我的 SharedPrefs,我试图在其中获取一些信息以添加到共享首选项:
public class SharedPrefs extends AppCompatActivity {
//public static Context context;
EditText ed1,ed2,ed3;
Button b1;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shared_prefs);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
b1=(Button)findViewById(R.id.button);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = ed1.getText().toString();
String ph = ed2.getText().toString();
String e = ed3.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
Toast.makeText(SharedPrefs.this,"Thanks",Toast.LENGTH_LONG).show();
Intent BackToMainIntent = new Intent(SharedPrefs.this, MainActivity.class);
startActivity(BackToMainIntent);
}
});
}
您似乎没有在第二个 activity 的任何地方保存布尔值 Check
。所以在你的第一个 activity 中,这个布尔值从不存在,你得到的是你设置为 false
的默认值。您需要在第二个 activity.
中将名为“Check”的首选项设置为 true
请在设置您想要的值后将 "Check" 键值添加到 SharedPreferences,因为它在 MainActivity 上始终 return false。
您也可以使用 .apply() 代替 .commit()。
在您的 SharedPrefs Activity
更改您的这段代码
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
有
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.putBooleon (Check,true);
editor.commit();
然后它会如你所愿地工作。
编码愉快:)
如有帮助,将不胜感激。我有两个活动。 MainActivity 和 SharedPrefs。我希望打开该应用程序以查看是否有一些已保存的首选项。如果不是,它必须转到另一个 activity 那里询问一些细节。然后在按下提交按钮时,它必须保存共享首选项并转到 MainActivity。但它没有,它只是跳回到 SharedPrefs activity。现在我不知道我的共享首选项是否没有被保存(我认为 "editor.commit" 会完成这项工作),或者我的循环有问题。我确实改变了我的循环,它正在以相反的方式工作,但我可能会把整个事情弄错,因为我对 Android 和 Java 很陌生。就像我说的,一些帮助将不胜感激。
这是我的 MainActivity 和我的循环:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
boolean check = sharedPreferences.getBoolean("Check",false);
if(!check){
//Intent intent;
Intent SharedPrefsIntent = new Intent(MainActivity.this, SharedPrefs.class);
startActivity(SharedPrefsIntent); }
else {
setContentView(R.layout.activity_main);
TextView brands = (TextView) findViewById(R.id.brands);
brands.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent brandsIntent = new Intent(MainActivity.this, brands.class);
startActivity(brandsIntent);
}
});
}
}
这是我的 SharedPrefs,我试图在其中获取一些信息以添加到共享首选项:
public class SharedPrefs extends AppCompatActivity {
//public static Context context;
EditText ed1,ed2,ed3;
Button b1;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shared_prefs);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
b1=(Button)findViewById(R.id.button);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = ed1.getText().toString();
String ph = ed2.getText().toString();
String e = ed3.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
Toast.makeText(SharedPrefs.this,"Thanks",Toast.LENGTH_LONG).show();
Intent BackToMainIntent = new Intent(SharedPrefs.this, MainActivity.class);
startActivity(BackToMainIntent);
}
});
}
您似乎没有在第二个 activity 的任何地方保存布尔值 Check
。所以在你的第一个 activity 中,这个布尔值从不存在,你得到的是你设置为 false
的默认值。您需要在第二个 activity.
请在设置您想要的值后将 "Check" 键值添加到 SharedPreferences,因为它在 MainActivity 上始终 return false。
您也可以使用 .apply() 代替 .commit()。
在您的 SharedPrefs Activity 更改您的这段代码
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.commit();
有
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(Email, e);
editor.putBooleon (Check,true);
editor.commit();
然后它会如你所愿地工作。
编码愉快:)