Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让我用Android登录_Android_Checkbox_Sharedpreferences - Fatal编程技术网

让我用Android登录

让我用Android登录,android,checkbox,sharedpreferences,Android,Checkbox,Sharedpreferences,我正在创建一个android应用程序,我已经为它创建了登录和主页。我想在登录页面中实现“让我保持登录”功能。我试过了,但还是不能很好地得到结果。我想我在订购代码时遇到了问题 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().set

我正在创建一个android应用程序,我已经为它创建了登录和主页。我想在登录页面中实现“让我保持登录”功能。我试过了,但还是不能很好地得到结果。我想我在订购代码时遇到了问题

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);


    SharedPreferences settings1 = getSharedPreferences(PREFS_NAME, 0);
    isChecked= settings1.getBoolean("isChecked", false);

    if (isChecked) {
        Intent rememberMe = new Intent(LoginPage.this, HomePageActivity.class);
        startActivity(rememberMe);
    } else {
        setContentView(R.layout.activity_login_page);
    }


    // Retrieving SharedPreferences
    sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
    // Retrieving editor
    editor = sharedPreferences.edit();

    rememberMeCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            SharedPreferences settings = getSharedPreferences(PREFS_NAME,
                    0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("isChecked", isChecked);
            editor.commit();
        }
    });

用户打开应用程序后,检查共享首选项是否包含用户详细信息键。如果用户选择保持我登录,则存储密钥

protected void onCreate(Bundle savedInstanceState) {
    shrd=getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
    if(shrd.contains("username")==true && shrd.contains("password")==true)
    {

        Intent in=new Intent(LoginScreen.this,MainActivity.class);
        startActivity(in);
        finish();


    }
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);

但是仍然不能很好地得到结果。我想我在对代码进行排序时遇到了一个问题。请解释更多使用当前代码时的行为。我想我应该使用片段来获得结果!不需要去寻找碎片。只需解释问题就可以从我们那里获得帮助在调用startActivity之后,您可能希望完成当前活动并从onCreate返回-否则代码将在不调用setContentView的情况下继续。