From 757b702c311360be14160d635344cf5aad7a397c Mon Sep 17 00:00:00 2001 From: YQ1997 <1340984967@qq.com> Date: Sat, 2 Jun 2018 01:32:30 +0800 Subject: [PATCH] =?UTF-8?q?#5=20#981=20=E7=AC=AC=E4=BA=94=E6=AC=A1?= =?UTF-8?q?=E5=AE=9E=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../androidlabs/hzuapps/tk/Main2Activity.java | 98 +++++++++++++++++- .../app/src/main/res/layout/activity_main.xml | 26 +++-- .../src/main/res/layout/activity_main2.xml | 23 ++++ .../src/main/res/layout/activity_main3.xml | 20 ++++ .../\345\256\236\351\252\214\344\272\224.md" | 30 ++++++ ...\345\256\236\351\252\214\344\272\2241.jpg" | Bin 0 -> 49521 bytes ...\345\256\236\351\252\214\344\272\2242.jpg" | Bin 0 -> 59970 bytes ...345\256\236\351\252\214\344\272\2243 .jpg" | Bin 0 -> 42609 bytes 8 files changed, 178 insertions(+), 19 deletions(-) create mode 100644 "Soft1614080902301/\345\256\236\351\252\214\344\272\224.md" create mode 100644 "Soft1614080902301/\345\256\236\351\252\214\344\272\2241.jpg" create mode 100644 "Soft1614080902301/\345\256\236\351\252\214\344\272\2242.jpg" create mode 100644 "Soft1614080902301/\345\256\236\351\252\214\344\272\2243 .jpg" diff --git a/Soft1614080902301/app/src/main/java/edu/androidlabs/hzuapps/tk/Main2Activity.java b/Soft1614080902301/app/src/main/java/edu/androidlabs/hzuapps/tk/Main2Activity.java index 413960d4a..ffbcca579 100644 --- a/Soft1614080902301/app/src/main/java/edu/androidlabs/hzuapps/tk/Main2Activity.java +++ b/Soft1614080902301/app/src/main/java/edu/androidlabs/hzuapps/tk/Main2Activity.java @@ -1,13 +1,101 @@ package edu.androidlabs.hzuapps.tk; +import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; public class Main2Activity extends AppCompatActivity { + private Button mButtonSave; + private Button mButtonDisplay; + private EditText mEditTextContent; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main2); + mEditTextContent = (EditText) findViewById(R.id.editText2); + mButtonSave = (Button) findViewById(R.id.button3); + mButtonSave.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + //向内部存储写入数据 + saveContent(); + Toast.makeText(Main2Activity.this, "saved!", Toast.LENGTH_SHORT).show(); + + } + }); + } + + private String getContent() { + String s = null; + FileInputStream fis = null; + try { + //通过该方法从内部存储中的edit_data.txt文件中读取数据 + fis = this.openFileInput("edit_data.txt"); + int len = 0; + byte[] buf = new byte[1024]; + while ((len = fis.read(buf)) != -1) { + s = new String(buf, 0, len, "UTF-8"); + + } + + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return s; + } + + //写入数据 + private void saveContent() { + FileOutputStream fos = null; + String content = mEditTextContent.getText().toString(); - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main2); + try { + + + //通过该方法向内部存储中写入数据,文件名为edit_data.txt,模式为MODE_PRIVATE,表示该文件操作权限为文本应用,另一个常用的权限MODE_APPEND表示在文件末尾添加内容 + fos = this.openFileOutput("edit_data.txt", MODE_PRIVATE); + fos.write(content.getBytes()); + fos.flush(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (fos != null) { + try { + fos.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + } } -} + + + diff --git a/Soft1614080902301/app/src/main/res/layout/activity_main.xml b/Soft1614080902301/app/src/main/res/layout/activity_main.xml index 4525432c6..932db57ed 100644 --- a/Soft1614080902301/app/src/main/res/layout/activity_main.xml +++ b/Soft1614080902301/app/src/main/res/layout/activity_main.xml @@ -7,21 +7,12 @@ tools:context=".MainActivity" tools:layout_editor_absoluteY="81dp"> - -