Skip to content

Commit

Permalink
#5 #903 第五次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
leixm committed May 13, 2018
1 parent 64625af commit cc49265
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class SecondActivity extends AppCompatActivity {

Expand All @@ -18,17 +25,78 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

xiazai=(Button)findViewById(R.id.button_xiazai);
xiazai=(Button)findViewById(R.id.button_xiazai);
xiazai.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())
write(s);

refreshHistoricalRecord(s);

Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
// startActivity(intent);
}
}

);
initHistoricalRecord();

}

private void refreshHistoricalRecord(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 initHistoricalRecord() //初始化记录
{
TextView textView = (TextView) findViewById(R.id.textview_history);
String s = read();
if(s!=null)
refreshHistoricalRecord(s);
}

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;
}

public void write(String msg){ //写出存储数据
// 步骤1:获取输入值
if(msg == null) return;
try {
// 步骤2:创建一个FileOutputStream对象,MODE_APPEND追加模式
msg += read(); //前面的也读进来
FileOutputStream fos = openFileOutput("storage.txt",
MODE_APPEND);
// 步骤3:将获取过来的值放入文件
fos.write(msg.getBytes());
// 步骤4:关闭数据流
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}
9 changes: 9 additions & 0 deletions soft1614080902217/app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
android:orientation="horizontal">

<EditText
android:id="@+id/edittext_input"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="请输入下载地址或者磁链接" />
Expand All @@ -32,6 +33,14 @@
android:textSize="50sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/textview_history"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="历史记录:"
/>



</LinearLayout>
Expand Down
Binary file added soft1614080902217/shiyan5/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions soft1614080902217/shiyan5/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
第五次实验

1.实验目标

(1)掌握在Android App中存储数据

2.实验步骤

(1)打开已建好的项目soft1614080902217

(2)在SecondActivity中加入存储功能,每一次下载都把下载地址记录到外部txt文件并且刷新记录,每次打开该界面时读入txt中的记录数据

(3)在activity_Second_xml中加入一个文本框用于显示历史记录

(4)完成并运行成功

(5)用git上交

## 3.实验结果
![image](https://github.com/curtain2017xxxx/android-labs-2018/blob/master/soft1614080902217/shiyan5/1.jpg)

## 4.实验体会

这次实验比较有意思,学会了简单的存储,利用java.io类提供的方法简单读写数据来存储历史记录,以后要更深入学习。

0 comments on commit cc49265

Please sign in to comment.