Skip to content

Commit

Permalink
Merge pull request #1952 from PickForSth/master
Browse files Browse the repository at this point in the history
#5 #1370 实验五
  • Loading branch information
zengsn authored May 14, 2018
2 parents 059fda7 + 6107927 commit ea0ab02
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Binary file added com1613090502214/#5Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added com1613090502214/#5data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package edu.hzuapps.androidlabs.com1613090502214;

import android.content.ContextWrapper;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class SecondActivity extends AppCompatActivity {
private TextView textView;
Expand All @@ -18,5 +27,36 @@ public void onClick(View v){
Intent intent=new Intent(SecondActivity.this,Com1613090502214Activity.class);
startActivity(intent);
}});

((Button) findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = ((EditText) findViewById(R.id.editText)).getText().toString();
String text1=((Spinner) findViewById(R.id.spinner)).getSelectedItem().toString();
save(text);
save(text1);
}
});
}

public void save(String text){
FileOutputStream out=null;
BufferedWriter writer=null;
try{
out=openFileOutput("demo", 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();
}
}
}
}

40 changes: 40 additions & 0 deletions com1613090502214/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
实验五
=
一.实验目的
-
掌握在Android App中存储数据

二.实验内容
-
根据选题要求使用文件存储

将应用产生的数据保存到文件存储中;<br>
说明使用的文件存储方式:内部 or 外部;<br>
将运行结果截图。<br>

三.实验步骤
-
1.Context提供了两个方法来打开数据文件里的文件IO流 FileInputStream openFileInput(String name);FileOutputStream(String name , int mode),<br>
这两个方法第一个参数 用于指定文件名,第二个参数指定打开文件的模式,这里我使用的是MODE_APPEND.指定文件名为demo.<br>
2.获取edittext和spinner的值.<br>
3.创建存储文件的函数save,这里我使用的是内部存储<br>
4.调用save函数,传递edittext和spinner的值.<br>
5.使用Git将代码提交到自己的库中:https://github.com/PickForSth/android-labs-2018<br>

$ git pull origin master<br>
$ git add com1613090502214<br>
$ git commit "#5 #1370第5次实验"<br>
$ git push<br>
6.在GitHub中使用Markdown文件编写实验报告(report5.md).<br>
7.在自己的GitHub库上创建和发送Pull Request(注意查看Changed files).<br>

四.实验截图
-
![screen](https://github.com/PickForSth/android-labs-2018/blob/master/com1613090502214/%235Screenshot.png)

![screen](https://github.com/PickForSth/android-labs-2018/blob/master/com1613090502214/%235data.png)

五.实验体会
-
通过这次实验使我更加熟悉git和android studio,获取edittext和spinner控件的值,将值内部存储到文件,创建事件监听器对button控件进行处理

0 comments on commit ea0ab02

Please sign in to comment.