gpt4 book ai didi

java - 如何修复共享首选项以保存我的数据并跳过 Activity ?

转载 作者:行者123 更新时间:2023-12-02 01:44:55 26 4
gpt4 key购买 nike

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

 import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.icu.text.IDNA;
import android.support.v4.media.RatingCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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()
{
tv_user = (TextView)findViewById(R.id.tv_user);
tv_pass = (TextView)findViewById(R.id.tv_pass);
tx_user = (EditText)findViewById(R.id.tx_user);
tx_pass = (EditText)findViewById(R.id.tx_pass);
btn_login = (Button)findViewById(R.id.btn_login);
img_ghorme = (ImageView)findViewById(R.id.img_ghorme);
}



public String save_user = "";
public String save_pass = "";

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

findall();



if(save_user.equals("admin") && save_pass.equals("admin"))
{

Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}


btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = tx_user.getText()+"";
String pass = tx_pass.getText()+"";

save_user = save.getString("username", "");
save_pass = save.getString("password", "");

SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();



if(user.equals("admin") && pass.equals("admin"))
{

Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);

}

if (!user.equals("admin") || !pass.equals("admin"))
{

e.putString("username", "");
e.putString("password", "");
e.apply();

tx_user.setText("");
tx_pass.setText("");
}
}
});



save = getSharedPreferences("user", MODE_PRIVATE);
save = getSharedPreferences("pass", MODE_PRIVATE);

Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");

tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);


Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);

tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);

tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);

tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);

tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);

btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);

img_ghorme.startAnimation(ani_dtu);

}

@Override
protected void onPause() {
super.onPause();
finish();
}
}
<小时/>
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"
android:background="@drawable/tx_bg"
android:drawableLeft="@drawable/ic_user"
android:ems="10"
android:hint="نام کاربری"
android:inputType="textPersonName"
android:padding="10dp"
android:text=""
android:textColor="@color/textShiri"
android:textColorHint="@color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_user" />

<TextView
android:id="@+id/tv_pass"
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_toBottomOf="@+id/tx_user" />

<EditText
android:id="@+id/tx_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="@drawable/tx_bg"
android:drawableLeft="@drawable/ic_pass"
android:ems="15"
android:hint="........."
android:inputType="textPassword"
android:padding="10dp"
android:text=""
android:textColor="@color/textShiri"
android:textColorHint="@color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_pass" />

<Button
android:id="@+id/btn_login"
android:layout_width="122dp"
android:layout_height="50dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="20dp"
android:background="@drawable/btn_bg"
android:text="ورود"
android:textColor="@color/textShiri"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/img_ghorme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tx_pass" />



</android.support.constraint.ConstraintLayout>

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

最佳答案

在使用sharedPreference并为每个 key 对定义两个sharedPreference之前,您没有初始化它。如下更改您的代码。

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()
{
tv_user = findViewById(R.id.tv_user);
tv_pass = findViewById(R.id.tv_pass);
tx_user = findViewById(R.id.tx_user);
tx_pass = findViewById(R.id.tx_pass);
btn_login = findViewById(R.id.btn_login);
img_ghorme = findViewById(R.id.img_ghorme);
}



public String save_user = "";
public String save_pass = "";

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

findall();

save = getSharedPreferences("userInfo", MODE_PRIVATE);
save_user = save.getString("username", "");
save_pass = save.getString("password", "");


if(save_user.equals("admin") && save_pass.equals("admin"))
{

Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}


btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String user = tx_user.getText().toString();
String pass = tx_pass.getText().toString();


SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();



if(user.equals("admin") && pass.equals("admin"))
{

Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);

}

if (!user.equals("admin") || !pass.equals("admin"))
{

e.putString("username", "");
e.putString("password", "");
e.apply();

tx_user.setText("");
tx_pass.setText("");
}
}
});


Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");

tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);


Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);

tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);

tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);

tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);

tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);

btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);

img_ghorme.startAnimation(ani_dtu);

}

@Override
protected void onPause() {
super.onPause();
finish();
}

}

关于java - 如何修复共享首选项以保存我的数据并跳过 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57465908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com