一、
在安卓里面,想要把数据永久保存下来(不至于退出程序数据就没了),可以使用SharedPreferences这个类来实现,和其他方式(文件保存方式、SQLite数据库保存)相比,比较简单。这里用的是SharedPreferences来简单地实现用户信息的保存。
二、
- SharedPreferences以键值对的方式来保存数据,读取数据时用SharedPreferences对象的get…()方法就可以了,但是保存数据的话需要用到SharedPreferences里面的Editor对象。
- 比如说这里的数据是保存在名为“info.xml文件中的”,这个文件可以用Android Studio右下角中的文件管理器“Device File Explorer”来查看。“info.xml文件位于“data/data/项目包名”下的shared_prefs目录中,里面的内容如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="password">123456</string>
<string name="username">zhangsan</string>
</map>
password、username是键的名称,123456和zhangsan是键对应的值。要获取值,是通过键名来获取的,这个java中的双列集合Map是一个道理。
三、代码
- java代码:
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private EditText accountEt,passwordEt;//账号、密码的输入框
private Button saveBtn,showBtn;//保存、显示信息的按钮
private SharedPreferences sharedPreferences;//共享参数
private String username;//输入的用户名
private String password;//输入的密码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化操作
accountEt=findViewById(R.id.accountEt);
passwordEt= findViewById(R.id.passwordEt);
saveBtn=findViewById(R.id.saveBtn);
showBtn=findViewById(R.id.showBtn);
//为保存、显示按钮设置监听事件
saveBtn.setOnClickListener(this);
showBtn.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.saveBtn:
username=accountEt.getText().toString().trim();//获取用户名输入框中的内容,用trim()去掉首尾空格
password=passwordEt.getText().toString().trim();
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password))
{
Toast.makeText(MainActivity.this,"用户名或密码不能为空!",Toast.LENGTH_SHORT).show();
}
else
{
sharedPreferences=getSharedPreferences("info",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("username",username);
editor.putString("password",password);
editor.commit();
Toast.makeText(MainActivity.this,"保存成功!",Toast.LENGTH_SHORT).show();
}
break;
case R.id.showBtn:
sharedPreferences=getSharedPreferences("info",MODE_PRIVATE);
username=sharedPreferences.getString("username","默认用户名");
password=sharedPreferences.getString("password","默认密码");
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("显示保存的信息");
builder.setMessage("用户名:"+username+"\n"+"密码:"+password);
builder.setPositiveButton("知道了", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
builder.setCancelable(false);
builder.create().show();//调用create()方法时返回的是一个AlertDialog对象,再调用show()方法将对话框显示出来
break;
default:
}
}
}
- 布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_title_bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/back"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_centerInParent="true"
android:textSize="22sp"
android:textColor="@color/top_title_text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_alignParentRight="true"
android:background="@null"
android:textSize="18sp"
android:textColor="#BC0B0A"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@drawable/item_bg_1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TextView
android:id="@+id/accountTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="账号:"
android:textColor="@android:color/background_dark" />
<EditText
android:id="@+id/accountEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/accountTv"
android:layout_marginLeft="3dp"
android:inputType="textEmailAddress"
android:singleLine="true"
android:ems="20"
android:maxLength="25"
android:textColor="@color/colorPrimary"
android:background="@null"
android:layout_marginTop="40dp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_bg_1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TextView
android:id="@+id/passwordTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="密码:"
android:textColor="@android:color/background_dark" />
<EditText
android:id="@+id/passwordEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/passwordTv"
android:layout_marginLeft="3dp"
android:inputType="textPassword"
android:singleLine="true"
android:ems="20"
android:hint="请输入密码"
android:maxLength="25"
android:textColor="@color/colorPrimary"
android:ellipsize="end"
android:background="@null"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?"
android:layout_alignParentRight="true"
android:textColor="#BC0B0A"
android:layout_marginRight="8dp"
android:padding="10dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center">
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:text="保存用户信息"/>
<Button
android:id="@+id/showBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:layout_margin="10dp"
android:text="显示用户信息"/>
</LinearLayout>
</LinearLayout>
四、截图
五、完整项目github地址
因为还需要用到一些图片资源,完整项目里面才有。
地址:项目的github地址