可以记住信息,即使退出也会保存下来,继续使用
简单的登录界面
源代码:
package com.example.hp.keepbaocun;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText userName,userPass;
CheckBox saveInfo;
SharedPreferences pref;
SharedPreferences.Editor editor;
Button btn_Login;
Button btn_Cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.user_name);
userPass = (EditText) findViewById(R.id.user_pass);
saveInfo = (CheckBox) findViewById(R.id.save_info);
Button btn_Login = (Button) findViewById(R.id.btn_login);
Button btn_Cancel = (Button) findViewById(R.id.btn_cancel);
//私密的模式存储XML文件,XML文件名为UserInfo
pref = getSharedPreferences("UserInfo",MODE_PRIVATE);
//两个参数,第一个参数是preferece的名称(比如:MyPref),第二个参数是打开的方式(一般选择private方式)
editor = pref.edit();
String name = pref.getString("userName","");
if (name == null){saveInfo.setChecked(false);}
else {saveInfo.setChecked(true);userName.setText(name);}
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_login:
String name = userName.getText().toString().trim();//trim()函数的功能是去掉首尾空格
String pass = userPass.getText().toString().trim();
if ("admin".equals(name) && "123456".equals(pass)){
if (saveInfo.isChecked()){
editor.putString("userName",name);
editor.commit();
}else {
editor.remove("userName");
editor.commit();
}
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this,"禁止登陆",Toast.LENGTH_LONG).show();
}
}
}
});
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context="com.example.hp.keepbaocun.MainActivity">
<TextView
android:text="用户名: "
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_usrname"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:ignore="HardcodedText,SpUsage" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/user_pass"
android:layout_below="@+id/user_name"
android:layout_alignLeft="@+id/user_name"
android:layout_alignStart="@+id/user_name"
android:layout_marginTop="15dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/user_name"
tools:ignore="HardcodedText"
android:layout_below="@+id/tv_usrname"
android:layout_toRightOf="@+id/tv_usrname"
android:layout_toEndOf="@+id/tv_usrname"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp" />
<TextView
android:text="密码:"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_usrpass"
tools:ignore="HardcodedText,SpUsage"
android:layout_below="@+id/user_name"
android:layout_alignRight="@+id/tv_usrname"
android:layout_alignEnd="@+id/tv_usrname" />
<CheckBox
android:text="保存信息"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/save_info"
android:layout_below="@+id/user_pass"
android:layout_alignLeft="@+id/tv_usrpass"
android:layout_alignStart="@+id/tv_usrpass"
android:layout_marginTop="13dp"
android:checked="false"
tools:ignore="HardcodedText" />
<Button
android:text="登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/save_info"
android:layout_alignLeft="@+id/save_info"
android:layout_alignStart="@+id/save_info"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_marginTop="18dp"
android:id="@+id/btn_login"
tools:ignore="HardcodedText" />
<Button
android:text="取消"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:id="@+id/btn_cancel"
android:layout_alignBaseline="@+id/btn_login"
android:layout_alignBottom="@+id/btn_login"
android:layout_toRightOf="@+id/btn_login"
android:layout_toEndOf="@+id/btn_login"
tools:ignore="HardcodedText" />
</RelativeLayout>