[Android新手学习笔记34]-Storage-SharedPreferences

来源:互联网 发布:php去掉所有html标签 编辑:程序博客网 时间:2024/06/04 01:15

使用键值对存储,值有类型区分。

  1. public class MainActivity extends AppCompatActivity {
  2.    @Override
  3.    protected void onCreate(Bundle savedInstanceState) {
  4.        super.onCreate(savedInstanceState);
  5.        setContentView(R.layout.activity_main);
  6.        Button saveButton = (Button) findViewById(R.id.save_button);
  7.        saveButton.setOnClickListener(new View.OnClickListener() {
  8.            @Override
  9.            public void onClick(View v) {
  10.                // 存数据
  11.                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
  12.                editor.putString("name", "张3");
  13.                editor.putInt("age", 24);
  14.                editor.putBoolean("married", false);
  15.                editor.apply(); // 提交数据
  16.                // 读数据
  17.                SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
  18.                String name = pref.getString("name", "");
  19.                int age = pref.getInt("age", 0);
  20.                boolean married = pref.getBoolean("married", false);
  21.                Log.d("xxd", "Name:" + name + ", age:" + age + ", married:" + married);
  22.            }
  23.        });
  24.    }
  25. }

默认路径:/data/data/<packagename>/files/,格式.xml。

记录账号密码:

  1. public class MainActivity extends AppCompatActivity {
  2.    private SharedPreferences preferences;
  3.    private SharedPreferences.Editor editor;
  4.    private EditText accountEdit;
  5.    private EditText passwordEdit;
  6.    private CheckBox rememberCheck;
  7.    private Button loginButton;
  8.    @Override
  9.    protected void onCreate(Bundle savedInstanceState) {
  10.        super.onCreate(savedInstanceState);
  11.        setContentView(R.layout.activity_main);
  12.        preferences = PreferenceManager.getDefaultSharedPreferences(this);
  13.        accountEdit = (EditText) findViewById(R.id.account_edit);
  14.        passwordEdit = (EditText) findViewById(R.id.password_edit);
  15.        rememberCheck = (CheckBox) findViewById(R.id.remember_password);
  16.        loginButton = (Button) findViewById(R.id.login_button);
  17.        boolean isRemember = preferences.getBoolean("remember_password", false);
  18.        if (isRemember) {
  19.            // 将账号密码设置上
  20.            String account = preferences.getString("account", "");
  21.            String password = preferences.getString("password", "");
  22.            accountEdit.setText(account);
  23.            passwordEdit.setText(password);
  24.            rememberCheck.setChecked(true);
  25.        }
  26.        loginButton.setOnClickListener(new View.OnClickListener() {
  27.            @Override
  28.            public void onClick(View v) {
  29.                String account = accountEdit.getText().toString();
  30.                String password = passwordEdit.getText().toString();
  31.                if (account.equals("admin") && password.equals("123456")) {
  32.                    editor = preferences.edit();
  33.                    if (rememberCheck.isChecked()) {
  34.                        editor.putBoolean("remember_password", true);
  35.                        editor.putString("account", account);
  36.                        editor.putString("password", password);
  37.                    } else {
  38.                        editor.clear();
  39.                    }
  40.                    editor.apply();
  41.                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  42.                } else {
  43.                    Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
  44.                }
  45.            }
  46.        });
  47.    }
  48. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <LinearLayout
  8.        android:orientation="horizontal"
  9.        android:layout_width="match_parent"
  10.        android:layout_height="60dp">
  11.        <TextView
  12.            android:text="账号:"
  13.            android:textSize="18sp"
  14.            android:gravity="center_vertical"
  15.            android:layout_width="wrap_content"
  16.            android:layout_height="match_parent" />
  17.        <EditText
  18.            android:id="@+id/account_edit"
  19.            android:gravity="center_vertical"
  20.            android:hint="请输入账号"
  21.            android:textSize="18sp"
  22.            android:layout_width="0dp"
  23.            android:layout_weight="1"
  24.            android:layout_height="match_parent" />
  25.    </LinearLayout>
  26.    <LinearLayout
  27.        android:orientation="horizontal"
  28.        android:layout_width="match_parent"
  29.        android:layout_height="60dp">
  30.        <TextView
  31.            android:text="密码:"
  32.            android:textSize="18sp"
  33.            android:gravity="center_vertical"
  34.            android:layout_width="wrap_content"
  35.            android:layout_height="match_parent" />
  36.        <EditText
  37.            android:id="@+id/password_edit"
  38.            android:gravity="center_vertical"
  39.            android:inputType="textPassword"
  40.            android:hint="请输入密码"
  41.            android:textSize="18sp"
  42.            android:layout_width="0dp"
  43.            android:layout_weight="1"
  44.            android:layout_height="match_parent" />
  45.    </LinearLayout>
  46.    <LinearLayout
  47.        android:orientation="horizontal"
  48.        android:layout_width="match_parent"
  49.        android:layout_height="60dp">
  50.        <CheckBox
  51.            android:id="@+id/remember_password"
  52.            android:layout_width="wrap_content"
  53.            android:layout_height="wrap_content" />
  54.        <TextView
  55.            android:text="记住密码"
  56.            android:textSize="18sp"
  57.            android:gravity="center_vertical"
  58.            android:layout_width="wrap_content"
  59.            android:layout_height="match_parent" />
  60.    </LinearLayout>
  61.    <Button
  62.        android:id="@+id/login_button"
  63.        android:text="登录"
  64.        android:layout_width="match_parent"
  65.        android:layout_height="60dp" />
  66. </LinearLayout>

0 0
原创粉丝点击