使用SharedPreferences將姓名和年齡信息保存在文件中,並讀取信息
阿新 • • 發佈:2017-05-09
new sta text str @override this ket make and
第一個是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="請輸入姓名" 15android: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 }
這樣就可以使程序正常運行。
使用SharedPreferences將姓名和年齡信息保存在文件中,並讀取信息