Skip to content

Commit

Permalink
Merge pull request #2207 from CAIYB/master
Browse files Browse the repository at this point in the history
#5 #1429 实验5
  • Loading branch information
zengsn authored May 23, 2018
2 parents 5cc345c + 53ece8c commit 5c83926
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Com1614080901103Activity extends AppCompatActivity {

private ImageView imageView;
private Button newbutton;
private EditText textBox;
private static final int READ_BLOCK_SIZE=100;
@Override
protected void onCreate(Bundle savedInstanceState) {

Expand All @@ -26,12 +35,65 @@ public void onClick(View v) {
startActivity(intent);
}
});
newbutton=(Button) findViewById(R.id.button);
newbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Com1614080901103Activity.this,SecondActivity.class);
startActivity(intent);
textBox=(EditText) findViewById(R.id.txtText1);
Button saveBtn=(Button)findViewById(R.id.btnSave);
Button loadBtn=(Button)findViewById(R.id.btnLoad);

saveBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String str=textBox.getText().toString();
try
{
FileOutputStream fOut=
openFileOutput("textfile.txt",MODE_WORLD_READABLE);
OutputStreamWriter osw=new OutputStreamWriter(fOut);
//-------将字符串写入文件----
osw.write(str);
osw.flush();
osw.close();

//-------显示文件被保存的消息----
Toast.makeText(getBaseContext(),"File saved successfully",
Toast.LENGTH_SHORT).show();
//-------清空EditText----
textBox.setText("");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
});

loadBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
try
{
FileInputStream fIn=
openFileInput("textfile.txt");
InputStreamReader isr=new InputStreamReader(fIn);

char[] inputBuffer=new char[READ_BLOCK_SIZE];
String s="";

int charRead;
while((charRead=isr.read(inputBuffer))>0)
{
//-------将字符串转换成字符串------
String readString=
String.copyValueOf(inputBuffer,0,charRead);
s+=readString;

inputBuffer=new char[READ_BLOCK_SIZE];
}
//-------将EditText设置为已读取的文本----
textBox.setText(s);
Toast.makeText(getBaseContext(),"File load successfully!",
Toast.LENGTH_SHORT).show();
}
catch (IOException ioe){
ioe.printStackTrace();
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@
tools:context="androidlabs.hzuapps.edu.com1614080901103.Com1614080901103Activity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="471dp"
android:background="#f0f0f0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">


<TextView
android:layout_gravity="center"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欢迎来到网上大学app"
android:layout_width="fill_parent"
android:text="请输入要查找的课程"
android:textColor="#f023f0" />

<EditText
android:id="@+id/txtText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="下一页" />

android:text="Save" />
<Button
android:id="@+id/btnLoad"
android:text="Load"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/textview_01"
android:layout_width="match_parent"
Expand Down
27 changes: 27 additions & 0 deletions com1614080901103/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 第四次实验 :Android存储编程

## 1. 实验目标
掌握在Android App中存储数据

## 2. 实验内容
(1)将应用产生的数据保存到文件存储中;
(2)说明使用的文件存储方式:内部;
(3)将运行结果截图。


## 3. 实验步骤
(1).在第一个Activity的xml文件中新添加上一个EditView和Button,改变原来的TextView和Button
(2).在第一个Activity的java文件中使用内部存储的方法,将两个Buttion分别用来Save和Load

## 4. 实验结果
在搜索栏中输入课程
![image](https://github.com/CAIYB/android-labs-2018/blob/master/com1614080901103/%E5%AE%9E%E9%AA%8C5%E7%9A%84%E6%88%AA%E5%9B%BE1.png)
点击Save保存后,EditView中的内容会消失
![image](https://github.com/CAIYB/android-labs-2018/blob/master/com1614080901103/%E5%AE%9E%E9%AA%8C5%E7%9A%84%E6%88%AA%E5%9B%BE2.png)
点击Load后,保存的东西会重新出现
![image](https://github.com/CAIYB/android-labs-2018/blob/master/com1614080901103/%E5%AE%9E%E9%AA%8C5%E7%9A%84%E6%88%AA%E5%9B%BE3.png)
## 5. 实验体会
**这次实验拖的时间挺久的,一开始是不知道如何去做,看了老师的提示也是懵懂的,在上网查看了Android的
存储后,才知道它们有什么区别,知道了自己要做成什么,后面在查看了书后,知道了如何去实现,发现其实也
不算难。只是使用到了输入输出流,这些之前没有怎么学过只是知道有这个东西,这次的实验收获了这个东西的使用,
感觉也是挺有趣的。**
Binary file added com1614080901103/实验5的截图1.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 com1614080901103/实验5的截图2.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 com1614080901103/实验5的截图3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c83926

Please sign in to comment.