创建一个新的Android工程,创建界面如图所示,完成内部存储的读写功能。在MainActivity中单击“保存数据到文件”按钮,将班级+姓名+学号保存至“data.txt”文件中,单击“从文件中读取数据”按钮,则读取存储数据的“data.txt”,将其中的内容显示在界面中
时间: 2024-05-04 22:16:46 浏览: 14
好的,我明白了你的要求,我会给你提供一些代码示例,供你参考。
首先,你需要在你的 Android 项目中创建一个布局文件,用于显示界面。以下是一个基本的布局文件示例,你可以根据自己的需求进行修改:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="班级:" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入班级" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓名:" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学号:" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学号" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存数据到文件" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从文件中读取数据" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从文件中读取的数据:" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
接下来,在你的 MainActivity 类中,你需要添加以下代码,用于实现读写文件的功能:
```java
public class MainActivity extends AppCompatActivity {
private EditText mEditText1;
private EditText mEditText2;
private EditText mEditText3;
private TextView mTextView5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText1 = findViewById(R.id.editText1);
mEditText2 = findViewById(R.id.editText2);
mEditText3 = findViewById(R.id.editText3);
mTextView5 = findViewById(R.id.textView5);
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = mEditText1.getText().toString() + ","
+ mEditText2.getText().toString() + ","
+ mEditText3.getText().toString();
try {
FileOutputStream fos = openFileOutput("data.txt", MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileInputStream fis = openFileInput("data.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
String data = new String(buffer);
String[] dataArray = data.split(",");
mTextView5.setText("班级:" + dataArray[0] + "\n姓名:" + dataArray[1] + "\n学号:" + dataArray[2]);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
```
以上代码中,我们首先在 onCreate() 方法中获取了界面中需要用到的一些控件,比如 EditText、TextView 和 Button 等。然后,在第一个按钮的点击事件中,我们将班级、姓名和学号拼接成一个字符串,然后将该字符串写入文件中。在第二个按钮的点击事件中,我们读取存储数据的“data.txt”文件,并将其中的内容显示在界面中。
最后,别忘了在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
这样,你就可以完成内部存储的读写功能了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)