使用键值对存储,值有类型区分。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button saveButton = (Button) findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 存数据
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "张3");
editor.putInt("age", 24);
editor.putBoolean("married", false);
editor.apply(); // 提交数据
// 读数据
SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
String name = pref.getString("name", "");
int age = pref.getInt("age", 0);
boolean married = pref.getBoolean("married", false);
Log.d("xxd", "Name:" + name + ", age:" + age + ", married:" + married);
}
});
}
}
默认路径:/data/data/<packagename>/files/,格式.xml。
记录账号密码:
public class MainActivity extends AppCompatActivity {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private CheckBox rememberCheck;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
accountEdit = (EditText) findViewById(R.id.account_edit);
passwordEdit = (EditText) findViewById(R.id.password_edit);
rememberCheck = (CheckBox) findViewById(R.id.remember_password);
loginButton = (Button) findViewById(R.id.login_button);
boolean isRemember = preferences.getBoolean("remember_password", false);
if (isRemember) {
// 将账号密码设置上
String account = preferences.getString("account", "");
String password = preferences.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberCheck.setChecked(true);
}
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();
if (account.equals("admin") && password.equals("123456")) {
editor = preferences.edit();
if (rememberCheck.isChecked()) {
editor.putBoolean("remember_password", true);
editor.putString("account", account);
editor.putString("password", password);
} else {
editor.clear();
}
editor.apply();
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:text="账号:"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="@+id/account_edit"
android:gravity="center_vertical"
android:hint="请输入账号"
android:textSize="18sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:text="密码:"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="@+id/password_edit"
android:gravity="center_vertical"
android:inputType="textPassword"
android:hint="请输入密码"
android:textSize="18sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<CheckBox
android:id="@+id/remember_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="记住密码"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
<Button
android:id="@+id/login_button"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
作者:a191030148 发表于2017/2/18 23:43:22 原文链接
阅读:35 评论:0 查看评论