如何修复 sharedpreference 以保存我的数据并跳过活动?

我想使用“共享首选项”来保存我的用户名和密码,它已保存但在我第二次打开该应用程序时不起作用。

XML:


<?xml version="1.0" encoding="utf-8"?>

     <android.support.constraint.ConstraintLayout 

      xmlns:android="http://schemas.android.com/apk/res/android"

      xmlns:app="http://schemas.android.com/apk/res-auto"

      xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.home.seesion10.Login_page"

android:layoutDirection="rtl"

android:textDirection="rtl"

android:background="@color/textShiri">


<ImageView

    android:id="@+id/img_ghorme"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    app:srcCompat="@drawable/cirlcleghormesabzi"

    android:layout_marginTop="1500dp"

    app:layout_constraintTop_toTopOf="parent"

    android:layout_marginLeft="100dp"

    app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintHorizontal_bias="0.0"

    app:layout_constraintVertical_bias="0.902"

    android:layout_marginStart="100dp" />


<TextView

    android:id="@+id/tv_user"

    android:layout_width="0dp"

    android:layout_height="wrap_content"

    android:layout_marginLeft="8dp"

    android:layout_marginRight="15dp"

    android:layout_marginTop="20dp"

    android:text="نام کاربری"

    android:textSize="15sp"

    android:visibility="invisible"

    app:layout_constraintHorizontal_bias="0.0"

    app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent"

    app:layout_constraintTop_toTopOf="parent" />


<EditText

    android:id="@+id/tx_user"

    android:layout_width="0dp"

    android:layout_height="wrap_content"

    android:layout_marginLeft="80dp"

    android:layout_marginRight="30dp"

    android:layout_marginTop="10dp"

<Button

    android:id="@+id/btn_login"


 </android.support.constraint.ConstraintLayout>

我希望当我为用户名和密码编写 admin admin 时,它会保存它,并且我第二次打开应用程序时它会跳过该登录页面但它不起作用并再次显示登录页面


呼啦一阵风
浏览 128回答 1
1回答

呼如林

您没有在使用 sharedPreference 之前初始化它,也没有为每个密钥对定义两个 sharedPreferences。如下更改您的代码。public class Login_page extends AppCompatActivity {TextView tv_user, tv_pass;EditText tx_user, tx_pass;Button btn_login;ImageView img_ghorme;SharedPreferences save;public void findall(){&nbsp; &nbsp; tv_user = findViewById(R.id.tv_user);&nbsp; &nbsp; tv_pass = findViewById(R.id.tv_pass);&nbsp; &nbsp; tx_user = findViewById(R.id.tx_user);&nbsp; &nbsp; tx_pass = findViewById(R.id.tx_pass);&nbsp; &nbsp; btn_login = findViewById(R.id.btn_login);&nbsp; &nbsp; img_ghorme = findViewById(R.id.img_ghorme);}public String save_user = "";public String save_pass = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main2);&nbsp; &nbsp; findall();&nbsp; &nbsp; save = getSharedPreferences("userInfo", MODE_PRIVATE);&nbsp; &nbsp; save_user = save.getString("username", "");&nbsp; &nbsp; save_pass = save.getString("password", "");&nbsp; &nbsp; if(save_user.equals("admin") && save_pass.equals("admin"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Intent skip = new Intent(Login_page.this, food_page.class);&nbsp; &nbsp; &nbsp; &nbsp; startActivity(skip);&nbsp; &nbsp; }&nbsp; &nbsp; btn_login.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String user = tx_user.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String pass = tx_pass.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences.Editor e = save.edit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.putString("username", user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.putString("password", pass);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.apply();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(user.equals("admin") && pass.equals("admin"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent food = new Intent(Login_page.this, food_page.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(food);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!user.equals("admin") || !pass.equals("admin"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.putString("username", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.putString("password", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.apply();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tx_user.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tx_pass.setText("");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");&nbsp; &nbsp; Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");&nbsp; &nbsp; Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");&nbsp; &nbsp; tv_user.setTypeface(font_shabnam_bold);&nbsp; &nbsp; tv_pass.setTypeface(font_shabnam_bold);&nbsp; &nbsp; tx_user.setTypeface(font_shabnam_light);&nbsp; &nbsp; tx_pass.setTypeface(font_shabnam_light);&nbsp; &nbsp; btn_login.setTypeface(font_shabnam_bold);&nbsp; &nbsp; Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);&nbsp; &nbsp; Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);&nbsp; &nbsp; Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);&nbsp; &nbsp; Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);&nbsp; &nbsp; tv_user.setVisibility(View.VISIBLE);&nbsp; &nbsp; tv_user.startAnimation(ani_rtl);&nbsp; &nbsp; tv_pass.setVisibility(View.VISIBLE);&nbsp; &nbsp; tv_pass.startAnimation(ani_rtl);&nbsp; &nbsp; tx_user.setVisibility(View.VISIBLE);&nbsp; &nbsp; tx_user.startAnimation(ani_ltr);&nbsp; &nbsp; tx_pass.setVisibility(View.VISIBLE);&nbsp; &nbsp; tx_pass.startAnimation(ani_ltr);&nbsp; &nbsp; btn_login.setVisibility(View.VISIBLE);&nbsp; &nbsp; btn_login.startAnimation(ani_fade);&nbsp; &nbsp; img_ghorme.startAnimation(ani_dtu);}@Overrideprotected void onPause() {&nbsp; &nbsp; super.onPause();&nbsp; &nbsp; finish();}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java