实验效果图
退出后重新进入项目效果图
开发步骤
1.建立UI界面
<TextView
android:id="@+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/tvName"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvUsername"
android:layout_below="@+id/tvUsername"
android:background="@android:drawable/edit_text"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etUsername"
android:layout_below="@+id/etUsername"
android:text="@string/tvPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvPassword"
android:layout_below="@+id/tvPassword"
android:layout_marginTop="16dp"
android:background="@android:drawable/edit_text"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/etPassword"
android:layout_below="@+id/etPassword"
android:layout_marginTop="20dp"
android:background="#FF72CAE1"
android:onClick="save"
android:text="@string/btnLogin" />
<CheckBox
android:id="@+id/cbKeeppsd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etPassword"
android:layout_alignTop="@+id/btnLogin"
android:layout_marginLeft="2dp"
android:textSize="22dp"
android:checked="true"
android:text="@string/keeppsd" />
<TextView
android:id="@+id/tvRegist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="18dp"
android:text="@string/tvRegister"
android:autoLink="all"
android:textColorLink="#FF0066CC" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="24dp"
android:src="@drawable/panda" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:src="@drawable/icon" />
2.美化布局界面
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:startColor="#FFACDAE5"
android:endColor="#FF72CAE1"
android:angle="45"
/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#55FFFFFF"/>
<corners android:radius="10dp"/>
</shape>
3.SharedPreferences 用来保存数据
3.1 获取.SharedPreferences对象
preferences = getSharedPreferences("data", MODE_PRIVATE);
3.2 存入数据
cbKeeppsd.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (cbKeeppsd.isChecked()) {
preferences.edit().putBoolean("rem_pwd", true).commit();
} else {
preferences.edit().putBoolean("rem_pwd", false).commit();
}
}
});
3.3 判断账号密码是否正确
if (userName.equals("zhangsan") && password.equals("123")) {
Toast.makeText(MainActivity.this, "登录成功",
Toast.LENGTH_SHORT).show();
3.4记住账号密码
if (cbKeeppsd.isChecked()) {
Editor editor = preferences.edit();
editor.putString("USER_NAME", userName);
editor.putString("PASSWORD", password);
editor.commit();
}
3.5 界面跳转
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("name", userName);
startActivity(intent);
3.6 重新登录时 读取数据
public void read() {
if (preferences.getBoolean("rem_pwd", false)) {
cbKeeppsd.setChecked(true);
etUserName.setText(preferences.getString("USER_NAME", ""));
etPassword.setText(preferences.getString("PASSWORD", ""));
}
}