Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

#5 #53 实验4.5代码以及报告 #1641

Merged
merged 18 commits into from
Jan 10, 2021
Merged
Prev Previous commit
Next Next commit
#5 #53 实验五报告
  • Loading branch information
JeesionChone authored Jan 8, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit eb352bbd40b4bed7bfef55f8b8671d12409354ac
98 changes: 98 additions & 0 deletions students/net1814080903310/lab5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# 实验五

## 1.实验目标
1. 了解Andoid的存储手段;
2. 掌握Android的文件存储;
3. 掌握Android的数据库存储。

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

## 3.实验步骤
1. 用sharepreference实现资料卡的存储与读取
```
package edu.hzuapps.androidlabs.students.net1814080903310;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import edu.hzuapps.androidlabs.R;
public class Information1814080903310Activity extends AppCompatActivity {
private EditText PersonName;
private EditText EmailAddress;
private EditText Phone;
private Button write;
private Button read;
private Context This;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info1814080903310);
PersonName = (EditText)findViewById(R.id.editTextTextPersonName);
EmailAddress = (EditText)findViewById(R.id.editTextTextEmailAddress);
Phone = (EditText)findViewById(R.id.editTextPhone);
read = (Button)findViewById(R.id.read);
write = (Button)findViewById(R.id.write);
}
public void onClick(View view){
String mPersonName = PersonName.getText().toString();
String mEmailAddress = EmailAddress.getText().toString();
String mPhone = Phone.getText().toString();
switch (view.getId()){
case R.id.write:
msave(mPersonName,mEmailAddress,mPhone);
break;
case R.id.read:
mread();
break;
}
}
private void mread(){
SharedPreferences sp = getSharedPreferences("info",MODE_PRIVATE);
String mPersonName = sp.getString("1","");
String mEmailAddress = sp.getString("2","");
String mPhone = sp.getString("3","");
PersonName.setText("");
EmailAddress.setText("");
Phone.setText("");
Toast.makeText(this,"昵称为:"+mPersonName+"邮箱为:"+mEmailAddress+"电话为:"+mPhone,Toast.LENGTH_LONG).show();
}
private boolean msave(String PersonName,String EmailAddress,String Phone){
SharedPreferences sp = getSharedPreferences("info",MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("1",PersonName);
edit.putString("2",EmailAddress);
edit.putString("3",Phone);
edit.apply();
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
return true;
}
}
```

## 4.实验结果
![存储资料卡](https://github.com/JeesionChone/android-labs-2020/blob/master/students/net1814080903310/lab5_1.png)
![读取资料卡](https://github.com/JeesionChone/android-labs-2020/blob/master/students/net1814080903310/lab5_2.png)

## 5.实验心得
这次试验用的SharedPreferences来实现资料卡的存储与读取,SharedPreferences方法为内部存储的方法.