帮我写一个安卓的记事本
时间: 2023-07-16 07:12:27 浏览: 126
好的,下面是一个基本的安卓记事本的代码示例:
activity_main.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|start"
android:inputType="textMultiLine"
android:minLines="10"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
```
MainActivity.java:
```java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
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 Activity {
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = 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) {
switch (item.getItemId()) {
case R.id.action_save:
saveFile();
return true;
case R.id.action_load:
loadFile();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void saveFile() {
String fileName = "my_file.txt";
String content = mEditText.getText().toString();
try {
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
Toast.makeText(this, "File saved successfully!", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
private void loadFile() {
String fileName = "my_file.txt";
try {
FileInputStream fis = openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
mEditText.setText(sb.toString());
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
menu_main.xml:
```xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_save"
android:icon="@android:drawable/ic_menu_save"
android:title="@string/action_save"
android:showAsAction="ifRoom" />
<item
android:id="@+id/action_load"
android:icon="@android:drawable/ic_menu_recent_history"
android:title="@string/action_load"
android:showAsAction="ifRoom" />
</menu>
```
这个记事本的功能很简单,只有两个按钮,一个用于保存文件,一个用于加载文件。当用户按下保存按钮时,它将文件保存到设备的内部存储器中。当用户按下加载按钮时,它将从设备的内部存储器中加载文件并将其显示在EditText中。
请注意,这只是一个基本的示例,可能需要根据您的实际需求进行修改和扩展。
阅读全文