1 回答
TA贡献1798条经验 获得超3个赞
您没有在使用 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()
{
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();
}
}
添加回答
举报