android自动登陆SharedPreferences
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private Button login;
private EditText username;
private EditText password;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private CheckBox remember;
private String usernamevalue;
private String passwordvalue;
@Overrideprotected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); pref = PreferenceManager.getDefaultSharedPreferences(this); username = (EditText) findViewById(R.id.e_username); password = (EditText) findViewById(R.id.e_password); login = (Button) findViewById(R.id.login); remember = (CheckBox) findViewById(R.id.checkbox); boolean isremember = pref.getBoolean("remember", false); if (isremember) {//若选择了记住密码,则下次登陆直接进入登陆后页面 remember.setChecked(true); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); Log.e("LoginActivity", "自动登陆"); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { usernamevalue = username.getText().toString(); passwordvalue = password.getText().toString(); if (usernamevalue.equals("admin") && passwordvalue.equals("123456")) { editor = pref.edit(); if (remember.isChecked()) { editor.putBoolean("remember", true); editor.putString("username", usernamevalue); editor.putString("password", passwordvalue); } else { editor.clear(); } editor.apply(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "username or password is invalid", Toast.LENGTH_SHORT).show(); } } });}
}
acitivity_login.xml
<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.period.password.LoginActivity">
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_marginTop="20dp">
android:layout_height="wrap_content"
android:text="UserName"
android:layout_weight="4"
android:gravity="center"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="15dp"
/>
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
android:layout_height="wrap_content"
android:text="Password:"
android:layout_weight="4"
android:gravity="center"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="15dp"
/>
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:padding="10dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:layout_height="wrap_content"
android:text="Remember password"/>
MainActivity.java //登陆成功后跳转的页面
public class MainActivity extends AppCompatActivity {
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);}
}
activity_main.xml
android:layout_height="match_parent"
android:text="welcome!"/>
更多相关文章
- 安全新手入坑——HTML标签
- Nginx系列教程(四)| 一文带你读懂Nginx的动静分离
- Android架构组件_LiveData
- Viewpager2—登录注册引导页面
- Android抓包方法(一)之Fiddler代理
- android 重新加载网络页面设置
- 【腾讯Bugly干货分享】Android内存泄漏的简单检查与分析方法
- Android(安卓)中Scheme协议的使用详解
- android html唤醒APP(原生)