android studio开发记事本代码
时间: 2023-10-04 09:04:49 浏览: 147
以下是一个简单的 Android Studio 记事本应用的代码示例:
MainActivity.java:
```java
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editText;
String fileName = "note.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
loadNote();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_save) {
saveNote();
return true;
} else if (id == R.id.action_delete) {
deleteNote();
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadNote() {
try {
FileInputStream inputStream = openFileInput(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
editText.setText(stringBuilder.toString());
inputStream.close();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error loading note", Toast.LENGTH_SHORT).show();
}
}
private void saveNote() {
try {
FileOutputStream outputStream = openFileOutput(fileName, MODE_PRIVATE);
outputStream.write(editText.getText().toString().getBytes());
outputStream.close();
Toast.makeText(getApplicationContext(), "Note saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error saving note", Toast.LENGTH_SHORT).show();
}
}
private void deleteNote() {
File file = new File(getFilesDir(), fileName);
if (file.exists()) {
file.delete();
editText.setText("");
Toast.makeText(getApplicationContext(), "Note deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "No note to delete", Toast.LENGTH_SHORT).show();
}
}
}
```
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|left"
android:padding="10dp"
android:scrollbars="vertical"/>
</LinearLayout>
```
menu_main.xml:
```xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_save"
android:title="Save"
android:icon="@drawable/ic_save"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_delete"
android:title="Delete"
android:icon="@drawable/ic_delete"
app:showAsAction="ifRoom"/>
</menu>
```
这个应用程序有一个简单的文本编辑器界面,可以保存和删除文本文件。当应用程序启动时,它会尝试加载名为“note.txt”的文件。当用户点击保存按钮时,它将文本保存到名为“note.txt”的文件中,当用户点击删除按钮时,它将删除名为“note.txt”的文件。
阅读全文