zoukankan      html  css  js  c++  java
  • 使用SharedPreferences将姓名和年龄信息保存在文件中,并读取信息

    第一个是XML文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:id="@+id/activity_main"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="vertical"
     8     tools:context="com.example.zhoushasha.myapplication.MainActivity">
     9 
    10     <EditText
    11         android:id="@+id/et1"
    12         android:layout_width="wrap_content"
    13         android:layout_height="wrap_content"
    14         android:hint="请输入姓名"
    15         android:textSize="25dp"/>
    16     <EditText
    17         android:id="@+id/et2"
    18         android:layout_width="wrap_content"
    19         android:layout_height="wrap_content"
    20         android:hint="请输入年龄"
    21         android:textSize="25dp"/>
    22     <LinearLayout
    23         android:layout_width="wrap_content"
    24         android:layout_height="wrap_content"
    25         android:layout_weight="0">
    26         <Button
    27             android:id="@+id/bt1"
    28             android:layout_width="208dp"
    29             android:layout_height="wrap_content"
    30             android:text="写入"
    31             android:textSize="25dp"/>
    32 
    33         <Button
    34             android:id="@+id/bt2"
    35             android:layout_width="168dp"
    36             android:layout_height="wrap_content"
    37             android:text="读出"
    38             android:textSize="25dp"/>
    39     </LinearLayout>
    40 </LinearLayout>

    第二个是Java文件:

     1 package com.example.zhoushasha.myapplication;
     2 
     3 import android.app.Activity;
     4 import android.content.Context;
     5 import android.content.SharedPreferences;
     6 import android.content.SharedPreferences.Editor;
     7 import android.support.v7.app.AppCompatActivity;
     8 import android.os.Bundle;
     9 import android.view.View;
    10 import android.widget.Button;
    11 import android.widget.EditText;
    12 import android.widget.Toast;
    13 
    14 public class MainActivity extends AppCompatActivity {
    15     private EditText nameText;
    16     private EditText ageText;
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22         nameText = (EditText)this.findViewById(R.id.et1);
    23         ageText = (EditText)this.findViewById(R.id.et2);
    24         Button button = (Button)this.findViewById(R.id.bt1);
    25         Button showButton = (Button)this.findViewById(R.id.bt2);
    26         button.setOnClickListener(listener);
    27         showButton.setOnClickListener(listener);
    28 
    29         // 回显
    30         SharedPreferences sharedPreferences=getSharedPreferences("王艳",
    31                 Activity.MODE_PRIVATE);
    32         String nameValue = sharedPreferences.getString("name", "");
    33         int ageValue = sharedPreferences.getInt("age",18);
    34         nameText.setText(nameValue);
    35         ageText.setText(String.valueOf(ageValue));
    36     }
    37 
    38     private View.OnClickListener listener = new View.OnClickListener(){
    39         public void onClick(View v) {
    40             Button button = (Button)v;
    41             //ljq123文件存放在/data/data/<package name>/shared_prefs目录下
    42             SharedPreferences sharedPreferences=getSharedPreferences("王艳",
    43                     Activity.MODE_PRIVATE );
    44             switch (button.getId()) {
    45                 case R.id.bt1:
    46                     String name = nameText.getText().toString();
    47                     int age = Integer.parseInt(ageText.getText().toString());
    48                     Editor editor = sharedPreferences.edit(); //获取编辑器
    49                     editor.putString("name","王艳");
    50                     editor.putInt("age", age);
    51                     editor.commit();//提交修改
    52                     Toast.makeText( MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
    53                     break;
    54                 case R.id.bt2:
    55                     String nameValue = sharedPreferences.getString("name", "");
    56                     int ageValue = sharedPreferences.getInt("age", 1);
    57                     Toast.makeText(MainActivity.this,"姓名:"+nameValue + ",年龄:" + ageValue,Toast.LENGTH_LONG).show();
    58 
    59                     break;
    60             }
    61         }
    62     };
    63 }


    这样就可以使程序正常运行。
  • 相关阅读:
    【Codeforces 349B】Color the Fence
    【Codeforces 459D】Pashmak and Parmida's problem
    【Codeforces 467C】George and Job
    【Codeforces 161D】Distance in Tree
    【Codeforces 522A】Reposts
    【Codeforces 225C】Barcode
    【Codeforces 446A】DZY Loves Sequences
    【Codeforces 429B】Working out
    【Codeforces 478C】Table Decorations
    【Codeforces 478C】Table Decorations
  • 原文地址:https://www.cnblogs.com/zhoushasha/p/6832957.html
Copyright © 2011-2022 走看看