我编写了共享的首选项代码,它对我不起作用,问题是什么?

发布于 2025-01-23 15:02:43 字数 2807 浏览 3 评论 0原文

我编写了共享的首选项代码,它对我不起作用,我希望当名称为学术和密码为123时,我希望它移至特定的活动,我希望当名称是入学和入学时,我希望它移至另一个活动密码是456。问题在哪里?

//这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    login = findViewById(R.id.btnLogin);
    name = findViewById(R.id.inputUsername);
    password = findViewById(R.id.inputPassword);
    checkBox = findViewById(R.id.checkBox);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences preferences = getSharedPreferences("login data", MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            if (checkBox.isChecked()) {

                editor.putString("username", name.getText().toString());
                editor.putString("MyPassword", password.getText().toString());
                editor.putBoolean("checked", true);
                editor.apply();

            } else {
                editor.remove("username");
                editor.remove("MyPassword");
                editor.remove("checked");
                editor.apply();


            }
            if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
                Toast.makeText(LoginActivity.this, "empty data", Toast.LENGTH_SHORT).show();
            }

            if (name.getText().toString().equals(true_user_name_Admission) && password.getText().toString().equals(true_password_Admission)) {

                Intent l = new Intent(LoginActivity.this, AdmissionDashboardActivity.class);
                startActivity(l);
                Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();


            } else {
                if (name.getText().toString().equals(true_user_name_Academic) && password.getText().toString().equals(true_password_Academic)) {

                    Intent ad = new Intent(LoginActivity.this, AcademicDashboardActivity.class);
                    startActivity(ad);
                    Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(LoginActivity.this, "Required", Toast.LENGTH_SHORT).show();
                    name.setError("Required");
                    password.setError("Required");
                }

            }

        }

    });
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences sp = getSharedPreferences("login data", MODE_PRIVATE);
    name.setText(sp.getString("username", ""));
    password.setText(sp.getString("MyPassword", ""));
    checkBox.setChecked(sp.getBoolean("checked", false));

}

}

I wrote the shared preferences code and it did not work for me, I want it to move to a specific activity when the name is academic and the password is 123, and I want it to move to another activity when the name is admission and the password is 456. Where is the problem?

//this is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    login = findViewById(R.id.btnLogin);
    name = findViewById(R.id.inputUsername);
    password = findViewById(R.id.inputPassword);
    checkBox = findViewById(R.id.checkBox);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences preferences = getSharedPreferences("login data", MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            if (checkBox.isChecked()) {

                editor.putString("username", name.getText().toString());
                editor.putString("MyPassword", password.getText().toString());
                editor.putBoolean("checked", true);
                editor.apply();

            } else {
                editor.remove("username");
                editor.remove("MyPassword");
                editor.remove("checked");
                editor.apply();


            }
            if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
                Toast.makeText(LoginActivity.this, "empty data", Toast.LENGTH_SHORT).show();
            }

            if (name.getText().toString().equals(true_user_name_Admission) && password.getText().toString().equals(true_password_Admission)) {

                Intent l = new Intent(LoginActivity.this, AdmissionDashboardActivity.class);
                startActivity(l);
                Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();


            } else {
                if (name.getText().toString().equals(true_user_name_Academic) && password.getText().toString().equals(true_password_Academic)) {

                    Intent ad = new Intent(LoginActivity.this, AcademicDashboardActivity.class);
                    startActivity(ad);
                    Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(LoginActivity.this, "Required", Toast.LENGTH_SHORT).show();
                    name.setError("Required");
                    password.setError("Required");
                }

            }

        }

    });
}

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences sp = getSharedPreferences("login data", MODE_PRIVATE);
    name.setText(sp.getString("username", ""));
    password.setText(sp.getString("MyPassword", ""));
    checkBox.setChecked(sp.getBoolean("checked", false));

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2025-01-30 15:02:43

你能尝试这个吗

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

login = findViewById(R.id.btnLogin);
name = findViewById(R.id.inputUsername);
password = findViewById(R.id.inputPassword);
checkBox = findViewById(R.id.checkBox);
SharedPreferences preferences = getSharedPreferences("login data", MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();

login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       
        if (checkBox.isChecked()) {

            editor.putString("username", name.getText().toString());
            editor.putString("MyPassword", password.getText().toString());
            editor.putBoolean("checked", true);
            editor.apply();

        } 
        if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
            Toast.makeText(LoginActivity.this, "empty data", Toast.LENGTH_SHORT).show();
        }

        if (name.getText().toString().equals(true_user_name_Admission) && password.getText().toString().equals(true_password_Admission)) {

            Intent l = new Intent(LoginActivity.this, AdmissionDashboardActivity.class);
            startActivity(l);
            Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();


        } else if (name.getText().toString().equals(true_user_name_Academic) && password.getText().toString().equals(true_password_Academic)) {

                Intent ad = new Intent(LoginActivity.this, AcademicDashboardActivity.class);
                startActivity(ad);
                Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
            } else {

                Toast.makeText(LoginActivity.this, "Required", Toast.LENGTH_SHORT).show();
                name.setError("Required");
                password.setError("Required");
            }
    }

});
}

@Override
protected void onResume() {
    super.onResume();
    name.setText(sp.getString("username", ""));
    password.setText(sp.getString("MyPassword", ""));
    checkBox.setChecked(sp.getBoolean("checked", false));

}

can you try this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

login = findViewById(R.id.btnLogin);
name = findViewById(R.id.inputUsername);
password = findViewById(R.id.inputPassword);
checkBox = findViewById(R.id.checkBox);
SharedPreferences preferences = getSharedPreferences("login data", MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();

login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       
        if (checkBox.isChecked()) {

            editor.putString("username", name.getText().toString());
            editor.putString("MyPassword", password.getText().toString());
            editor.putBoolean("checked", true);
            editor.apply();

        } 
        if (TextUtils.isEmpty(name.getText().toString()) || TextUtils.isEmpty(password.getText().toString())) {
            Toast.makeText(LoginActivity.this, "empty data", Toast.LENGTH_SHORT).show();
        }

        if (name.getText().toString().equals(true_user_name_Admission) && password.getText().toString().equals(true_password_Admission)) {

            Intent l = new Intent(LoginActivity.this, AdmissionDashboardActivity.class);
            startActivity(l);
            Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();


        } else if (name.getText().toString().equals(true_user_name_Academic) && password.getText().toString().equals(true_password_Academic)) {

                Intent ad = new Intent(LoginActivity.this, AcademicDashboardActivity.class);
                startActivity(ad);
                Toast.makeText(LoginActivity.this, "Accessed", Toast.LENGTH_SHORT).show();
            } else {

                Toast.makeText(LoginActivity.this, "Required", Toast.LENGTH_SHORT).show();
                name.setError("Required");
                password.setError("Required");
            }
    }

});
}

@Override
protected void onResume() {
    super.onResume();
    name.setText(sp.getString("username", ""));
    password.setText(sp.getString("MyPassword", ""));
    checkBox.setChecked(sp.getBoolean("checked", false));

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文