布局文件登录 activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<EditText
android:layout_marginTop="20dp"
android:id="@+id/edit_name"
android:hint="请输入用户名"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edit_pwd"
android:hint="请输入密码"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/check_pwd"
android:text="记住密码"
android:layout_weight="1"
android:layout_marginLeft="30dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/check_login"
android:text="自动登录"
android:layout_weight="1"
android:layout_marginLeft="60dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button_login"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:text="我已阅读并同意服务条款"
android:layout_gravity="center"
android:layout_marginTop="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
布局文件 second
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:id="@+id/text_name"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_pwd"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_jizhupwd"
android:textSize="20sp"
android:text="查看是否为记住密码"
android:padding="8dp"
android:background="#ffff00"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private EditText edit_name;
private EditText edit_pwd;
private CheckBox check_pwd;
private Button button_login;
private SharedPreferences sp;
private CheckBox check_login;
private String passwordValue;
private String userNameValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intView();
initData();
}
private void initData() {
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userNameValue = edit_name.getText().toString();
passwordValue = edit_pwd.getText().toString();
boolean checked = check_pwd.isChecked();
SharedPreferences.Editor editor = sp.edit();
//保存用户名和密码
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD", passwordValue);
//是否记住密码
if (check_pwd.isChecked()) {
editor.putBoolean("cbRememberPass", true);
} else {
editor.putBoolean("cbRememberPass", false);
}
//是否自动登录
if (check_login.isChecked()) {
editor.putBoolean("autologin1", true);
} else {
editor.putBoolean("autologin1", false);
}
editor.commit();
Intent intent = new Intent(MainActivity.this, Second_Activity.class);
intent.putExtra("intentname", edit_name.getText().toString());
intent.putExtra("intentpwd", edit_pwd.getText().toString());
intent.putExtra("checked",checked);
startActivity(intent);
}
});
}
private void intView() {
sp = getSharedPreferences("userInfo", 0);
String name = sp.getString("USER_NAME", "");
String pass = sp.getString("PASSWORD", "");
boolean choseRemember = sp.getBoolean("cbRememberPass", false);
boolean choseAutoLogin = sp.getBoolean("autologin1", false);
edit_name = (EditText) findViewById(R.id.edit_name);
edit_pwd = (EditText) findViewById(R.id.edit_pwd);
check_pwd = (CheckBox) findViewById(R.id.check_pwd);
check_login = (CheckBox) findViewById(R.id.check_login);
button_login = (Button) findViewById(R.id.button_login);
//如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码
if (choseRemember) {
edit_name.setText(name);
edit_pwd.setText(pass);
check_pwd.setChecked(true);
}
if (choseAutoLogin) {
check_login.setChecked(true);
Intent intent = new Intent(MainActivity.this, Second_Activity.class);
intent.putExtra("intentname", edit_name.getText().toString());
intent.putExtra("intentpwd", edit_pwd.getText().toString());
startActivity(intent);
}
}
}
Second_Activity
public class Second_Activity extends Activity{
private TextView text_name;
private TextView text_pwd;
private TextView text_jizhupwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
initView();
initData();
}
private void initData() {
Intent intent = getIntent();
String name = intent.getStringExtra("intentname");
String pwd = intent.getStringExtra("intentpwd");
final boolean checked = intent.getBooleanExtra("checked",true);
text_name.setText(name);
text_pwd.setText(pwd);
text_jizhupwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checked)
{
Toast.makeText(Second_Activity.this,"您选择了记住密码!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(Second_Activity.this,"您没有选择记住密码!",Toast.LENGTH_SHORT).show();
}
}
});
}
private void initView() {
text_name = (TextView) findViewById(R.id.text_name);
text_pwd = (TextView) findViewById(R.id.text_pwd);
text_jizhupwd = (TextView) findViewById(R.id.text_jizhupwd);
}
}
实现自动登录和记住密码跳转传值