如何使用RegisterActivity中的SharedPreferences获取用户数据

时间:2017-04-16 00:36:57

标签: android sharedpreferences

我编写了我的注册活动以获取我的用户详细信息并将其存储为SharePreferences

现在,我试图获取存储在数据中的那些值,以便在我的应用程序中登录,但似乎我遗漏了一些东西,当我在我的登录布局中的edittexts中放入任何内容时,它会在不检查用户的情况下登录

RegisterActivity.class

public class RegistrarUsuario extends AppCompatActivity {


    private Button mBtnRegistrarUsuario;
    private TextView mRegistrarTxt;
    private EditText mUsername,mPassword,mSecondPassword;

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

        mRegistrarTxt = (TextView) findViewById(R.id.titulo2);
        mUsername = (EditText) findViewById(R.id.nombreUsuario);
        mPassword = (EditText) findViewById(R.id.primeraContraseña);
        mSecondPassword = (EditText) findViewById(R.id.segundaContraseña);

        Typeface fuente = Typeface.createFromAsset(getAssets(),"fonts/MrDafoe-Regular.ttf");
        mRegistrarTxt.setTypeface(fuente);


        mBtnRegistrarUsuario = (Button) findViewById(R.id.btnRegistrarUsuario);


        mBtnRegistrarUsuario.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SharedPreferences preference = getSharedPreferences("Reg",MODE_PRIVATE);
                String username = mUsername.getText().toString().trim();
                String password = mPassword.getText().toString().trim();
                String secondpassword = mSecondPassword.getText().toString().trim();




                if(username.length()<=0){
                    Toast.makeText(RegistrarUsuario.this, "Ingrese un usuario.", Toast.LENGTH_SHORT).show();

                }
                else if(password.length()<=0){

                    Toast.makeText(RegistrarUsuario.this, "Ingrese contraseña.", Toast.LENGTH_SHORT).show();
                }

                else if(secondpassword.length()<=0){
                    Toast.makeText(RegistrarUsuario.this, "Confirme su contraseña.", Toast.LENGTH_SHORT).show();

                }

                else if(password.equals(secondpassword)){

                    SharedPreferences.Editor editor = preference.edit();
                    editor.putString("Username",username);
                    editor.putString("Password",password);
                    editor.putString("SecondPassword",secondpassword);
                    editor.commit();

                    finish();
                    Toast.makeText(RegistrarUsuario.this, "Usuario creado con exito!", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegistrarUsuario.this,PantallaPrincipal.class);
                    startActivity(intent);

                }

                else{

                    Toast.makeText(RegistrarUsuario.this, "No coinciden las contraseñas.", Toast.LENGTH_SHORT).show();
                }




            }
        });

    }
}

** LoginActivity.class **

public class MainActivity extends AppCompatActivity {


    private EditText mUsername,mPassword;
    private Button mLoginBtn,mBtnRecuperar,mBtnRegistrar;
    private TextView mTextView;
    private static String usuario ="admin";
    private static String contraseña="123";


    private final String KEY_USERNAME = "username";

    private final String KEY_PASSWORD = "password";


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

        mUsername = (EditText) findViewById(R.id.usuario);
        mPassword = (EditText) findViewById(R.id.contraseña);
        mLoginBtn = (Button) findViewById(R.id.btnIngresar);
        mTextView = (TextView) findViewById(R.id.titulo);


        Typeface fuente = Typeface.createFromAsset(getAssets(),"fonts/MrDafoe-Regular.ttf");
        mTextView.setTypeface(fuente);


        mBtnRecuperar = (Button) findViewById(R.id.btnRecuperar);

        mBtnRecuperar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this,RecuperarContrasenia.class);
                startActivity(intent);
            }
        });

        mBtnRegistrar = (Button) findViewById(R.id.btnRegistrar);
        mBtnRegistrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this,RegistrarUsuario.class);
                startActivity(intent);

            }
        });




        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SharedPreferences preference = getSharedPreferences("Reg",MODE_PRIVATE);
                String username = mUsername.getText().toString();
                String password = mPassword.getText().toString();

                String userDetails = preference.getString(username + password + "data","No information on that user.");
                SharedPreferences.Editor editor = preference.edit();
                editor.putString("display",userDetails);
                editor.commit();

               if(mUsername.getText().toString().trim().length() == 0 && mPassword.getText().toString().trim().length() == 0 ){

                   Toast.makeText(MainActivity.this, "Los campos estan vacios", Toast.LENGTH_SHORT).show();
               }else
               {

                   if(mUsername.getText().toString().trim().equals(username) && mPassword.getText().toString().trim().equals(password)){

                       Toast.makeText(MainActivity.this, "Bienvenido", Toast.LENGTH_SHORT).show();
                       Intent intent = new Intent(MainActivity.this,PantallaPrincipal.class);
                       startActivity(intent);
                   }else{

                       Toast.makeText(MainActivity.this, "Los campos son incorrectos", Toast.LENGTH_SHORT).show();
                   }

               }




            }
        });


    }
}

我不知道我错过了它不会验证用户是否存在或使用我在RegisterActivity.class

中创建的凭据登录

感谢

1 个答案:

答案 0 :(得分:0)

您永远不会从Retrofit retrofit = new Retrofit.Builder() .baseUrl(SERVICE_ENDPOINT) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); 检索用户名和密码。最后,您将检查用户放置在TextEdits中的信息,以便验证始终将SharedPreferences返回到任何非空值。

改变这个:

true

对此:

SharedPreferences preference = getSharedPreferences("Reg",MODE_PRIVATE);
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();