主页面
package com.example.yinchenglong1601r20180521.view; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.example.yinchenglong1601r20180521.R; public class MainActivity extends AppCompatActivity { private EditText uname; private EditText psw; private Button tj; private CheckBox cb1; private CheckBox cb2; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 initView(); //使用键值对来判断是否选中记住密码和自动登录 if (sharedPreferences.getBoolean("RememberPassword", false)) { cb1.setChecked(true); uname.setText(sharedPreferences.getString("username", "")); psw.setText(sharedPreferences.getString("password", "")); } if (sharedPreferences.getBoolean("AutomaticLogin", false)) { cb2.setChecked(true); Intent intent = new Intent(MainActivity.this, Main2Activity.class); startActivity(intent); } jizhumima(); } private void jizhumima() { //选中记住密码时 cb1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cb1.isChecked()) { sharedPreferences.edit().putBoolean("RememberPassword", true).commit(); } else { sharedPreferences.edit().putBoolean("RememberPassword", false).commit(); } } }); //选中自动登录时也选中记住密码 cb2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cb2.isChecked()) { cb1.setChecked(true); sharedPreferences.edit().putBoolean("AutomaticLogin", true).commit(); } else { sharedPreferences.edit().putBoolean("AutomaticLogin", false).commit(); } } }); } private void initView() { //获取id uname = findViewById(R.id.et1); psw = findViewById(R.id.et2); tj = findViewById(R.id.tijiao); cb1 = findViewById(R.id.cb1); cb2 = findViewById(R.id.cb2); //使用SharedPreferences方法 sharedPreferences = getSharedPreferences("loginUser", Context.MODE_PRIVATE); //点击跳转 tj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判断文本框 login(v); //判断是否选中 if (cb1.isChecked()) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("username", uname.getText().toString()); editor.putString("password", psw.getText().toString()); editor.commit(); } else { uname.setText(""); psw.setText(""); } } }); } public void login(View view){ String name = uname.getText().toString();//用户名 String pwd = psw.getText().toString();//密码 StringBuilder stringBuilder = new StringBuilder(); boolean flag = true; if ("".equals(name) || "".equals(name.trim()) || name == null) { stringBuilder.append("手机号不能为空"); flag = false; } else { if (name.length() != 11) { stringBuilder.append("手机号长度为11位有效数字"); flag = false; } if (!name.startsWith("1")) { if (stringBuilder.length() > 0) { stringBuilder.append(",手机号应该以数字1开头"); } else { stringBuilder.append("手机号应该以数字1开头"); } flag = false; } } if ("".equals(pwd) || "".equals(pwd.trim()) || pwd == null) { if (stringBuilder.length() > 0) { stringBuilder.append(",密码不能为空"); } else { stringBuilder.append("密码不能为空"); } flag = false; }else if(pwd.length()!=6){ if (stringBuilder.length() > 0) { stringBuilder.append(",密码长度不正确"); } else { stringBuilder.append("密码长度不正确"); } flag = false; } if (flag == false) { Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show(); }else { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); finish(); } } }
主页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.yinchenglong1601r20180521.view.MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:"/> <EditText android:layout_width="180dp" android:layout_height="wrap_content" android:id="@+id/et1"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:"