-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2051 from xiaojiahao/master
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package androidlabs.hzuapps.edu.myapplication5; | ||
|
||
|
||
import android.content.ContextWrapper; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
import org.w3c.dom.Text; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
|
||
import android.view.View; | ||
import android.widget.Toast; | ||
|
||
public class MainActivity5 extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main5); | ||
|
||
((Button) findViewById(R.id.btn_save)). | ||
|
||
setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
String text = ((EditText) findViewById(R.id.et_info)).getText().toString(); | ||
save(text); | ||
} | ||
}); | ||
((Button) findViewById(R.id.btn_read)). | ||
|
||
setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
readsy5(); | ||
} | ||
}); | ||
|
||
|
||
} | ||
|
||
public void save(String text) { | ||
FileOutputStream out = null; | ||
BufferedWriter writer = null; | ||
try { | ||
out = openFileOutput("sy5.txt", ContextWrapper.MODE_APPEND); | ||
writer = new BufferedWriter(new OutputStreamWriter(out)); | ||
writer.write(text); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if (writer != null) { | ||
writer.close(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
Toast.makeText(MainActivity5.this, "数据保存成功", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
public void readsy5() { | ||
String content = ""; | ||
try { | ||
FileInputStream fis = openFileInput("sy5.txt"); | ||
byte[] buffer = new byte[fis.available()]; | ||
fis.read(buffer); | ||
content = new String(buffer); | ||
fis.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
Toast.makeText(MainActivity5.this, "保存的数据是" + content, Toast.LENGTH_SHORT).show(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity5"> | ||
|
||
<TextView | ||
android:id="@+id/textView1" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentTop="true" | ||
android:textSize="20dp" | ||
android:text="请输入您要存储的信息:" /> | ||
|
||
<EditText | ||
android:id="@+id/et_info" | ||
android:layout_width="wrap_content" | ||
android:layout_height="46dp" | ||
android:layout_alignParentLeft="true" | ||
android:layout_below="@+id/textView1" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:layout_marginLeft="8dp" | ||
android:layout_marginRight="8dp" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginTop="8dp" | ||
android:ems="10" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.0" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_bias="0.042" /> | ||
<requestFocus/> | ||
|
||
<Button | ||
android:id="@+id/btn_read" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignRight="@id/et_info" | ||
android:layout_below="@id/et_info" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginEnd="100dp" | ||
android:layout_marginLeft="8dp" | ||
android:layout_marginRight="100dp" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginTop="8dp" | ||
android:text="读取信息" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.585" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_bias="0.29" /> | ||
|
||
|
||
<Button | ||
android:id="@+id/btn_save" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentLeft="true" | ||
android:layout_below="@+id/et_info" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginEnd="280dp" | ||
android:layout_marginRight="280dp" | ||
android:layout_marginTop="8dp" | ||
android:text="保存信息" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_bias="0.29" /> | ||
|
||
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 第五次实验 | ||
|
||
## 1.实验目标 | ||
掌握在Android App中存储数据。 | ||
|
||
## 2.实验步骤 | ||
(1)、在res.layout中的activity5_main5.xml中设置一个TextView,一个EditText,两个button,并设置好id。 | ||
(2)、在MainActivity5.java中加入按钮监听,点击按钮保存数据时调用方法save,另一个为readsy5。 | ||
(3)、实现一个保存数据的方法save,用openFileOutput、OutputStreamWriter、BufferedWriter,其中的write等方法实现。 | ||
(4)、实现一个读取数据的方法readsy5,用FileInputStream、openFileInput和其中的read等方法实现。 | ||
(5)、记得关闭数据流使用.close,使用Toast.makeText通知保存数据成功和读取文件数据。由于使用MODE_APPEND,会将之前保存的数据一起读入。 | ||
## 3.实验结果 | ||
|
||
![image](https://github.com/xiaojiahao/android-labs-2018/blob/master/soft1614080902319/sy5(2).png) | ||
![image](https://github.com/xiaojiahao/android-labs-2018/blob/master/soft1614080902319/sy5(1).png) | ||
## 4.实验体会 | ||
这次实验参考了很多网上的代码,用老师的代码风格做了监听和保存数据,用网上的方法做了读取数据,成功运用了Toast.makeText。 | ||
以后再也不熬夜写文档了,没放到自己的文件夹下面,失策啊,以后会更加慎重的,对不起老师啊。 | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.