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: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"
android:orientation="vertical"
tools:context="com.example.period.password.LoginActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_marginTop="20dp">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UserName"
android:layout_weight="4"
android:gravity="center"
/>
android:id="@+id/e_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="15dp"
/>

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_weight="4"
android:gravity="center"/>
android:id="@+id/e_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginRight="15dp"
/>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:padding="10dp">
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remember password"/>

android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center"/>

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_width="match_parent"
android:layout_height="match_parent"
android:text="welcome!"/>

更多相关文章

  1. 安全新手入坑——HTML标签
  2. Nginx系列教程(四)| 一文带你读懂Nginx的动静分离
  3. Android架构组件_LiveData
  4. Viewpager2—登录注册引导页面
  5. Android抓包方法(一)之Fiddler代理
  6. android 重新加载网络页面设置
  7. 【腾讯Bugly干货分享】Android内存泄漏的简单检查与分析方法
  8. Android(安卓)中Scheme协议的使用详解
  9. android html唤醒APP(原生)

随机推荐

  1. android dm-verity 功能
  2. Android(安卓)菜单Menu学习
  3. Android(安卓)Systrace使用介绍
  4. VideoView控件可以手动改变大小
  5. android Hid 实现注意事项
  6. Android简易实战教程--第三十九话《简单
  7. java.lang.Exception: 密钥库文件不存在:
  8. 项目整合ActionBarSherlock,no android:ac
  9. 获取Android屏幕尺寸、控件尺寸、状态栏/
  10. Android NDK 运行错误:java.lang.Unsatisf