鱼C论坛

 找回密码
 立即注册
查看: 1581|回复: 0

[学习笔记] android 6.3.1 6.3.2

[复制链接]
发表于 2017-10-14 10:14:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这次与以往不同我在原来的项目上新建个activity,把activity_main.xml修改为
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">

  6.     <EditText
  7.         android:id="@+id/edit"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:hint="Type something here"
  11.          />
  12.     <Button
  13.         android:onClick="click"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:text="start second activity"/>
  17. </LinearLayout>
复制代码

把mainactivity.java修改为
  1. package com.example.xinwei.filepersistencetest;

  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.Toast;

  10. import java.io.BufferedReader;
  11. import java.io.BufferedWriter;
  12. import java.io.FileInputStream;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.io.OutputStreamWriter;

  18. public class MainActivity extends AppCompatActivity {
  19.                 private EditText edit;
  20.                 @Override
  21.                 protected void onCreate(Bundle savedInstanceState) {
  22.                     super.onCreate(savedInstanceState);
  23.                     setContentView(R.layout.activity_main);
  24.                     edit=(EditText)findViewById(R.id.edit);
  25.                     String inputText=load();
  26.                     if(TextUtils.isEmpty(edit.getText())){
  27.                         edit.setText(inputText);
  28.                         edit.setSelection(inputText.length());
  29.             Toast.makeText(this, "Restoring succeeded", Toast.LENGTH_SHORT).show();
  30.         }
  31.     }

  32.     @Override
  33.     protected void onDestroy() {
  34.         super.onDestroy();
  35.         String inputText=edit.getText().toString();
  36.         save(inputText);
  37.     }

  38.     private void save(String inputText) {
  39.         FileOutputStream out=null;
  40.         BufferedWriter writer=null;
  41.         try {
  42.             out=openFileOutput("data", Context.MODE_PRIVATE);
  43.             writer=new BufferedWriter(new OutputStreamWriter(out));
  44.             writer.write(inputText);
  45.         } catch (FileNotFoundException e) {
  46.             e.printStackTrace();
  47.         } catch (IOException e) {
  48.             e.printStackTrace();
  49.         }finally {
  50.             if(writer!=null){
  51.                 try {
  52.                     writer.close();
  53.                 } catch (IOException e) {
  54.                     e.printStackTrace();
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     public String load(){
  60.         FileInputStream in = null;
  61.         BufferedReader reader= null;
  62.         StringBuilder content = new StringBuilder();
  63.         try {
  64.             in = openFileInput("data");
  65.             reader = new BufferedReader(new InputStreamReader(in));
  66.             String line="";
  67.             while((line=reader.readLine())!=null){
  68.                 content.append(line);
  69.             }
  70.         } catch (FileNotFoundException e) {
  71.             e.printStackTrace();
  72.         } catch (IOException e) {
  73.             e.printStackTrace();
  74.         }finally {
  75.             if(reader!=null){
  76.                 try {
  77.                     reader.close();
  78.                 } catch (IOException e) {
  79.                     e.printStackTrace();
  80.                 }
  81.             }
  82.         }
  83.         return content.toString();
  84.     }
  85.     public void click(View v){
  86.         Intent intent = new Intent(this,SecondActivity.class);
  87.         startActivity(intent);
  88.     }
  89. }
复制代码

mainactivity.java旁新建SecondActiviy.java空活动,修改activity_second.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <Button
  7.     android:id="@+id/save_data"
  8.     android:layout_width="wrap_content"
  9.     android:layout_height="wrap_content"
  10.     android:text="save data"/>
  11.     <Button
  12.         android:id="@+id/restore_data"
  13.         android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         android:text="restore data"/>
  16. </LinearLayout>
复制代码

修改SecondActiviy.java文件
  1. package com.example.xinwei.filepersistencetest;

  2. import android.content.SharedPreferences;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.Toast;

  8. public class SecondActivity extends AppCompatActivity {

  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_second);
  13.         Button button=(Button)findViewById(R.id.save_data);
  14.         button.setOnClickListener(new View.OnClickListener() {
  15.             @Override
  16.             public void onClick(View view) {
  17.                 SharedPreferences.Editor editor=getSharedPreferences("data1",MODE_PRIVATE).edit();
  18.                 editor.putString("name","Tom");
  19.                 editor.putInt("age",28);
  20.                 editor.putBoolean("married",false);
  21.                 editor.apply();
  22.             }
  23.         });
  24.         restoreData();
  25.     }

  26.     private void restoreData() {
  27.         Button restoreData = (Button)findViewById(R.id.restore_data);
  28.         restoreData.setOnClickListener(new View.OnClickListener() {
  29.             @Override
  30.             public void onClick(View view) {
  31.                 SharedPreferences pref=getSharedPreferences("data1",MODE_PRIVATE);
  32.                 String name =pref.getString("name","");
  33.                 int age = pref.getInt("age",0);
  34.                 boolean married=pref.getBoolean("married",false);
  35.                 Toast.makeText(SecondActivity.this, name+age+married, Toast.LENGTH_SHORT).show();
  36.             }
  37.         });
  38.     }
  39. }
复制代码
在这个文件里有存储和读取数据的逻辑,都非常简单,效果图为:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 05:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表