繁体   English   中英

共享的偏好设置未保存或循环不起作用

[英]Shared preference not saved or loop does'nt work

一些帮助将是最感谢的。 我有两个活动。 MainActivity和SharedPrefs。 我希望打开的应用程序可以查看是否保存了一些偏好设置。 如果不是,则必须进行其他活动,要求提供一些详细信息。 然后,在按下提交按钮时,它必须保存共享的首选项并转到MainActivity。 但事实并非如此,它只是跳回到SharedPrefs活动。 现在,我不知道是否没有保存我的共享首选项(我认为“ 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);
        }
    });
}

设置所需的值后,请将“ Check”键值添加到SharedPreferences中,因为它在MainActivity上始终返回false。

您也可以使用.apply()代替.commit()。

您似乎没有在第二个活动中的任何地方保存布尔Check 因此,在您的第一个活动中,此布尔值永远不会存在,并且您将获得已设置为false的默认值。 您需要在第二个活动中将名为“检查”的首选项设置为true。

在您的SharedPrefs活动中,更改您的这段代码

        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();

然后,它将按您想要的方式工作。

快乐的编码:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM