不要从另一个班级

时间:2017-09-12 19:21:07

标签: android class preferences shared

如果设置了共享偏好设置,我试图让我的应用首先查看。如果没有,它必须打开你输入的页面,然后希望保存它们我将在以后使用。它看起来要么找到一些共享首选项,要么我的代码错误,因为主要活动打开(else语句执行)。

这是我的主要活动:

        public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    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);

这是SharedPrefs:

        public class SharedPrefs extends MainActivity {
//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();
        }
    });
      }

     }

我必须说实话,我不太确定我希望Java在哪里寻找共享首选项,"这使得应用程序至少运行。

1 个答案:

答案 0 :(得分:0)

SharedPrefs{}类中,声明变量

public static boolean CHECK_VALUE = false; 

onClick(View v)之后的editor.commit();方法内的同一个班级中,设置

CHECK_VALUE = true;

MainActivity{}OnCreate()方法

if(!SharedPrefs.CHECK_VALUE){
    //SharedPreferences was not used before
    Intent SharedPrefsIntent = new Intent(MainActivity.this, SharedPrefs.class);
    startActivity(SharedPrefsIntent);
  }
else {
//SharedPreference are already set
//Do your stuffs
}

实际上,您不需要在此处查看SharedPreference。如果你真的这样做,那就把

editor.putBoolean("check", true);

OnClick()类的SharedPrefs{}内部方法,并在MainActivity

中获取或使用它
SharedPreferences sharedPreferences = getSharedPreferences(SharedPrefs.MyPREFERENCES, Context.MODE_PRIVATE);
Boolean Check = sharedPreferences.getBoolean("check", false);
if(!check){
  //SharedPreferences was not used before
  Intent SharedPrefsIntent = new Intent(MainActivity.this, SharedPrefs.class);
  startActivity(SharedPrefsIntent);
}
 else {
   //SharedPreference are already set
   //Do your stuffs
}