android studio记事本app完整代码
时间: 2023-09-12 22:10:19 浏览: 192
以下是一个简单的 Android Studio 记事本应用的完整代码。这个应用包括基本的文本编辑功能,例如创建、打开、保存和编辑文本文件。
MainActivity.java
```
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
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 {
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = (EditText) findViewById(R.id.edit_text);
}
@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();
switch (id) {
case R.id.action_new:
newFile();
break;
case R.id.action_open:
openFile();
break;
case R.id.action_save:
saveFile();
break;
}
return super.onOptionsItemSelected(item);
}
private void newFile() {
mEditText.setText("");
Toast.makeText(this, "New file created", Toast.LENGTH_SHORT).show();
}
private void openFile() {
File file = getFileStreamPath("note.txt");
if (file.exists()) {
try {
FileInputStream fis = openFileInput("note.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
mEditText.setText(sb.toString());
br.close();
isr.close();
fis.close();
Toast.makeText(this, "File opened", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "File does not exist", Toast.LENGTH_SHORT).show();
}
}
private void saveFile() {
String text = mEditText.getText().toString();
try {
FileOutputStream fos = openFileOutput("note.txt", Context.MODE_PRIVATE);
fos.write(text.getBytes());
fos.close();
Toast.makeText(this, "File saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:inputType="textMultiLine"
android:scrollbars="vertical" />
</RelativeLayout>
```
menu_main.xml
```
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_new"
android:title="New"
android:icon="@android:drawable/ic_menu_add"
android:showAsAction="ifRoom" />
<item
android:id="@+id/action_open"
android:title="Open"
android:icon="@android:drawable/ic_menu_gallery"
android:showAsAction="ifRoom" />
<item
android:id="@+id/action_save"
android:title="Save"
android:icon="@android:drawable/ic_menu_save"
android:showAsAction="ifRoom" />
</menu>
```
阅读全文