【问题标题】:I want to save a username and password in shared preferences [duplicate]我想在共享首选项中保存用户名和密码 [重复]
【发布时间】:2018-03-27 15:32:30
【问题描述】:

我正在学习安卓开发。我正在制作一个注册和登录应用程序。我的注册和登录工作正常。但我不知道如何在共享偏好中保存用户名和密码。并设置一个布尔值以检查斜线屏幕以检查登录。 这是我的代码。

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private final AppCompatActivity activity = LoginActivity.this;

    private NestedScrollView nestedScrollView;

    private TextInputLayout textInputLayoutEmail;
    private TextInputLayout textInputLayoutPassword;

    private TextInputEditText textInputEditTextEmail;
    private TextInputEditText textInputEditTextPassword;

    private AppCompatButton appCompatButtonLogin;

    private AppCompatTextView textViewLinkRegister;

    private InputValidation inputValidation;
    private DatabaseHelper databaseHelper;
    AnimationDrawable animationDrawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initListeners();
        initObjects();
        animationDrawable=(AnimationDrawable)nestedScrollView.getBackground();
        animationDrawable.setEnterFadeDuration(1000);
        animationDrawable.setExitFadeDuration(1000);
        animationDrawable.start();

        //saving data in shared priference
        SharedPreferences sharedPreferences=getSharedPreferences("Login",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean("isLogin",true);
        editor.putString("user", String.valueOf(textInputEditTextEmail));
        editor.putString("pass", String.valueOf(textInputEditTextPassword));
        editor.commit();
    }

    /**
     * This method is to initialize views
     */
    private void initViews() {

        nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

        textInputLayoutEmail = (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
        textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);

        textInputEditTextEmail = (TextInputEditText) findViewById(R.id.textInputEditTextEmail);
        textInputEditTextPassword = (TextInputEditText) findViewById(R.id.textInputEditTextPassword);

        appCompatButtonLogin = (AppCompatButton) findViewById(R.id.appCompatButtonLogin);

        textViewLinkRegister = (AppCompatTextView) findViewById(R.id.textViewLinkRegister);

    }

    /**
     * This method is to initialize listeners
     */
    private void initListeners() {
        appCompatButtonLogin.setOnClickListener(this);
        textViewLinkRegister.setOnClickListener(this);
    }

    /**
     * This method is to initialize objects to be used
     */
    private void initObjects() {
        databaseHelper = new DatabaseHelper(activity);
        inputValidation = new InputValidation(activity);

    }

    /**
     * This implemented method is to listen the click on view
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.appCompatButtonLogin:
                verifyFromSQLite();
                break;
            case R.id.textViewLinkRegister:
                // Navigate to RegisterActivity
                Intent intentRegister = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(intentRegister);
                break;
        }
    }

    /**
     * This method is to validate the input text fields and verify login credentials from SQLite
     */
    private void verifyFromSQLite() {
        if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
            return;
        }
        if (!inputValidation.isInputEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
            return;
        }
        if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, getString(R.string.error_message_email))) {
            return;
        }

        if (databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim()
                , textInputEditTextPassword.getText().toString().trim())) {
            Intent accountsIntent = new Intent(activity, HomePage.class);
            accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
            emptyInputEditText();
            startActivity(accountsIntent);
            finish();
        } else {
            // Snack Bar to show success message that record is wrong
            Toast.makeText(getApplicationContext(),getString(R.string.error_valid_email_password),Toast.LENGTH_LONG).show();

        }
    }

    /**
     * This method is to empty all input edit text
     */
    private void emptyInputEditText() {
        textInputEditTextEmail.setText(null);
        textInputEditTextPassword.setText(null);
    }
}

这是我的斜线屏幕代码。

public class MainActivity extends AppCompatActivity {
    private static  int slash_screen_time_out=4000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
                finish();
            }
        },slash_screen_time_out);
    }
}

请帮我解决这个问题。 提前谢谢你。

【问题讨论】:

  • 请看看这个answer
  • 你的问题到底是什么?
  • 只是一个建议,您不应该将密码存储在设备上,您应该使用一些 authtoken 或 cookie 进行身份验证。
  • 如果用户登录一次并且使用后用户关闭应用程序,我想记住密码。但是当用户再次打开应用程序时,直接进入主页。
  • 它是什么? I want to save a username and password in shared preferences . 还是 I am a using a Database sqlite?您使用的是共享首选项还是 SQL 数据库?

标签: android android-sqlite sharedpreferences


【解决方案1】:

通过这种方式将数据保存在共享首选项中:

SharedPreferences settings = getSharedPreferences("Login", 0);
settings.edit().putString("user", username).apply();
...

并检查启动画面上的数据

String username = settings.getString("user", "");
if (username.isEmpty()){
   //.. data not saved
} else { 
 //.. data saved
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2015-10-11
    • 2018-02-08
    • 2019-10-03
    相关资源
    最近更新 更多