From a7b78faedd3ce62cba7a6e2fb03cb37cc1336c8c Mon Sep 17 00:00:00 2001 From: aLesion <717876241@qq.com> Date: Sat, 2 Jun 2018 21:49:27 +0800 Subject: [PATCH] =?UTF-8?q?#5=20=E6=83=A0=E5=A4=A7=E8=80=83=E8=AF=95?= =?UTF-8?q?=E5=A4=8D=E4=B9=A0=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../soft1614080902114/Main4Activity.java | 80 ++++++++++++++++++ .../src/main/res/layout/activity_main4.xml | 43 ++++++++++ shiyan5.png | Bin 0 -> 58569 bytes 3 files changed, 123 insertions(+) create mode 100644 Soft1614080902114/app/src/main/java/edu/hzuapps/android/soft1614080902114/Main4Activity.java create mode 100644 Soft1614080902114/app/src/main/res/layout/activity_main4.xml create mode 100644 shiyan5.png diff --git a/Soft1614080902114/app/src/main/java/edu/hzuapps/android/soft1614080902114/Main4Activity.java b/Soft1614080902114/app/src/main/java/edu/hzuapps/android/soft1614080902114/Main4Activity.java new file mode 100644 index 000000000..526230d25 --- /dev/null +++ b/Soft1614080902114/app/src/main/java/edu/hzuapps/android/soft1614080902114/Main4Activity.java @@ -0,0 +1,80 @@ +package com.example.soft1614080902114; + +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.TextView; + +import java.io.FileInputStream; +import java.io.FileOutputStream; + +public class Main4Activity extends AppCompatActivity { + + private Button seek; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main4); + seek=(Button)findViewById(R.id.button_seek); + seek.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final EditText tx= (EditText) findViewById(R.id.edittext_input); + String s = tx.getText().toString(); + if(!s.isEmpty()) + save(s); + refresh(s); + } + }); + init(); + } + + public void save(String msg){ + if(msg == null) return; + try { + msg += read(); + FileOutputStream fos = openFileOutput("storage.txt", + MODE_APPEND); + fos.write(msg.getBytes()); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public String read() { + try { + FileInputStream inStream = this.openFileInput("message.txt"); + byte[] buffer = new byte[1024]; + int hasRead = 0; + StringBuilder sb = new StringBuilder(); + while ((hasRead = inStream.read(buffer)) != -1) { + sb.append(new String(buffer, 0, hasRead)); + } + inStream.close(); + return sb.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + private void refresh(String msg) + { + if(msg==null) return; + TextView textView = (TextView) findViewById(R.id.textview_history); + String s = textView.getText().toString(); + textView.setText(s+"\n"+msg); + } + + private void init() + { + TextView textView = (TextView) findViewById(R.id.textview_history); + String s = read(); + if(s!=null) + refresh(s); + } + + } \ No newline at end of file diff --git a/Soft1614080902114/app/src/main/res/layout/activity_main4.xml b/Soft1614080902114/app/src/main/res/layout/activity_main4.xml new file mode 100644 index 000000000..23ff31d2b --- /dev/null +++ b/Soft1614080902114/app/src/main/res/layout/activity_main4.xml @@ -0,0 +1,43 @@ + + + + + + + + + +