android studio记事本源码
时间: 2023-05-01 15:00:44 浏览: 679
Android studio成品源码项目日历备忘录记事本,该日历备忘录app实现了日历查看,添加备忘录,闹钟提醒,删除备忘录等功
以下是一个简单的 Android Studio 记事本应用程序的源代码示例:
```java
package com.example.notebook;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
try {
FileInputStream fileInputStream = openFileInput("notes.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
editText.setText(stringBuilder.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void save(View view) {
try {
FileOutputStream fileOutputStream = openFileOutput("notes.txt", MODE_PRIVATE);
fileOutputStream.write(editText.getText().toString().getBytes());
fileOutputStream.close();
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请注意,这只是一个简单的示例,没有考虑许多实际应用程序中需要考虑的因素。
阅读全文