英文:
Save a checkbox state ( remember me) in android studio
问题
public class LoginActivity extends AppCompatActivity {
// ... (other imports and class definition)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// ... (other code)
Remember=findViewById(R.id.remember_me);
// ... (other code)
String email=sharedPreferences.getString("email","");
String passwords=sharedPreferences.getString("mot de passe","");
mEmail.setText(email);
mPassword.setText(passwords);
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// ... (other code)
if(Remember.isChecked()){
editor.putString("email",mEmail.getText().toString());
editor.putString("passowrd",mPassword.getText().toString());
editor.commit();
}else {
editor.putString("email", "");
editor.putString("passowrd", "");
editor.commit();
}
progressBar.setVisibility(View.VISIBLE);
// ... (other code)
}
});
// ... (other code)
}
}
<!-- activity_login.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bandeau_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:itemBackground="@color/noir"
app:itemIconTint="@drawable/selector"
app:itemTextColor="@drawable/selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/menu_navigation" />
<androidx.core.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="@+id/bandeau_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/saaalogo" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="16dp"
android:background="@color/noir"
android:ems="10"
android:fontFamily="@font/futura_medium"
android:hint="Email"
android:padding="13dp"
android:textColor="@color/blanc"
android:textColorHint="@color/blanc"
android:textSize="20sp" />
<EditText
android:id="@+id/password1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="16dp"
android:background="@color/noir"
android:ems="10"
android:fontFamily="@font/futura_medium"
android:hint="Mot de passe"
android:inputType="textPassword"
android:padding="13dp"
android:textColor="@color/blanc"
android:textColorHint="@color/blanc"
android:textSize="20sp" />
<CheckBox
android:id="@+id/remember_me"
android:layout_width="377dp"
android:layout_height="34dp"
android:layout_marginStart="16dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:text="Se Souvenir de moi" />
<!-- ... (other views) -->
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
英文:
I would Like to save my checkbox always checked after the first checking, when the user presses it , the thing works but when i exit the app the check box is reseted , is there a way to prevent this? Now I have to check it again every time I move out the page login:
I want also to Log in only one time sow I dont have to repeat it each time I go out the application or when I come back to the login page
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
EditText mEmail,mPassword;
TextView mCreateBtn,forgotTextLink;
Button mLoginBtn;
ProgressBar progressBar;
FirebaseAuth fAuth;
CheckBox Remember;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
BottomNavigationView bandeauNavigationView = findViewById(R.id.bandeau_navigation);
bandeauNavigationView.setSelectedItemId(R.id.accueil);
bandeauNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.accueil:
return true;
case R.id.commerce:
startActivity(new Intent(getApplicationContext(),PageCommercial.class));
overridePendingTransition(0,0);
return true;
case R.id.services:
startActivity(new Intent(getApplicationContext(),Support.class));
overridePendingTransition(0,0);
return true;
case R.id.info:
startActivity(new Intent(getApplicationContext(),Info.class));
overridePendingTransition(0,0);
return true;
}
return false;
}
});
mEmail = findViewById(R.id.email);
mPassword = findViewById(R.id.password1);
progressBar = findViewById(R.id.progressBar1);
fAuth = FirebaseAuth.getInstance();
mLoginBtn = findViewById(R.id.loginBtn1);
mCreateBtn = findViewById(R.id.createText1);
forgotTextLink = findViewById(R.id.forgotPassword);
Remember=findViewById(R.id.remember_me);
////////////////////////////////////
Remember.isChecked();
sharedPreferences=getSharedPreferences("LoginPrefs", MODE_PRIVATE);
editor=sharedPreferences.edit();
/////////////////To get Stored Data/////////////////////////////////
progressBar.setVisibility(View.GONE);
String email=sharedPreferences.getString("email","");
String passwords=sharedPreferences.getString("mot de passe","");
mEmail.setText(email);
mPassword.setText(passwords);
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
mEmail.setError("Email manquant.");
return;
}
if(TextUtils.isEmpty(password)){
mPassword.setError("Mot de passe manquant.");
return;
}
if(password.length() < 8){
mPassword.setError("Le mot de passe doit contenir au moins 8 caractères");
return;
}
if(Remember.isChecked()){
editor.putString("email",mEmail.getText().toString());
editor.putString("passowrd",mPassword.getText().toString());
editor.commit();
}else {
editor.putString("email", "");
editor.putString("passowrd", "");
editor.commit();
}
progressBar.setVisibility(View.VISIBLE);
// authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
startActivity(new Intent(LoginActivity.this,PageTechnique.class));
}else {
Toast.makeText(LoginActivity.this, "Erreur ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
});
mCreateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,Register.class));
}
});
forgotTextLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText resetMail = new EditText(v.getContext());
final AlertDialog.Builder passwordResetDialog= new AlertDialog.Builder(v.getContext());
passwordResetDialog.setTitle("Réinitialiser votre mot de passe ?");
passwordResetDialog.setMessage("Entrer votre Email pour recevoir un lien de réinitialisation");
passwordResetDialog.setView(resetMail);
passwordResetDialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// extract the email and send reset link
String mail = resetMail.getText().toString();
fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(LoginActivity.this, "Un lien de réinitialisation vous a été envoyé", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LoginActivity.this, "Erreur ! le lien de réinitialisation n'a pas été envoyé" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
passwordResetDialog.setNegativeButton("Non", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// close the dialog
}
});
passwordResetDialog.create().show();
}
});
}
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bandeau_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:itemBackground="@color/noir"
app:itemIconTint="@drawable/selector"
app:itemTextColor="@drawable/selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/menu_navigation" />
<androidx.core.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="@+id/bandeau_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/saaalogo" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="16dp"
android:background="@color/noir"
android:ems="10"
android:fontFamily="@font/futura_medium"
android:hint="Email"
android:padding="13dp"
android:textColor="@color/blanc"
android:textColorHint="@color/blanc"
android:textSize="20sp" />
<EditText
android:id="@+id/password1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="16dp"
android:background="@color/noir"
android:ems="10"
android:fontFamily="@font/futura_medium"
android:hint="Mot de passe"
android:inputType="textPassword"
android:padding="13dp"
android:textColor="@color/blanc"
android:textColorHint="@color/blanc"
android:textSize="20sp" />
<CheckBox
android:id="@+id/remember_me"
android:layout_width="377dp"
android:layout_height="34dp"
android:layout_marginStart="16dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:text="Se Souvenir de moi" />
<Button
android:id="@+id/loginBtn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="16dp"
android:background="@color/argent"
android:fontFamily="@font/futura_medium"
android:padding="15dp"
android:text="Se Connecter"
android:textSize="18sp" />
<TextView
android:id="@+id/createText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:fontFamily="@font/futura_medium"
android:padding="13dp"
android:text="Non inscrit ? Céer un compte"
android:textColor="@color/noir"
android:textSize="18sp"
android:visibility="visible" />
<TextView
android:id="@+id/forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:fontFamily="@font/futura_medium"
android:text="Mot de passe oublié ?"
android:textColor="@color/noir"
android:textSize="18sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="20dp"
android:visibility="visible" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
答案1
得分: 0
这个解决方案怎么样?
// 获取存储的数据
progressBar.setVisibility(View.GONE);
boolean isRemember = sharedPreferences.getBoolean("remember_me", false);
if (isRemember) {
String email = sharedPreferences.getString("email", "");
String passwords = sharedPreferences.getString("mot de passe", "");
mEmail.setText(email);
mPassword.setText(passwords);
Remember.setChecked(true);
}
mLoginBtn.setOnClickListener(new View.OnClickListener() {
// ...
if (Remember.isChecked()) {
editor.putBoolean("remember_me", true);
editor.putString("email", mEmail.getText().toString());
editor.putString("password", mPassword.getText().toString());
editor.commit();
} else {
// ...
英文:
How about this solution?
/////////////////To get Stored Data/////////////////////////////////
progressBar.setVisibility(View.GONE);
**boolean isRemember=sharedPreferences.getBoolean("remember_me",false);**
**if(isRemember){**
String email=sharedPreferences.getString("email","");
String passwords=sharedPreferences.getString("mot de passe","");
mEmail.setText(email);
mPassword.setText(passwords);
**Remember.setChecked(true);**
**}**
mLoginBtn.setOnClickListener(new View.OnClickListener() {
......
if(Remember.isChecked()){
**editor.putBoolean("remember_me",true);**
editor.putString("email",mEmail.getText().toString());
editor.putString("passowrd",mPassword.getText().toString());
editor.commit();
}else {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论