基于android studio的记事簿设计代码
时间: 2023-08-17 17:15:18 浏览: 100
以下是基于Android Studio的记事簿设计的代码示例,包括布局文件和Java代码:
1. 布局文件activity_main.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/noteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入笔记内容"/>
<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"/>
<ListView
android:id="@+id/noteListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
2. Java代码MainActivity.java:
```
public class MainActivity extends AppCompatActivity {
private EditText noteEditText;
private Button saveButton;
private ListView noteListView;
private List<String> noteList;
private ArrayAdapter<String> noteAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteEditText = findViewById(R.id.noteEditText);
saveButton = findViewById(R.id.saveButton);
noteListView = findViewById(R.id.noteListView);
noteList = new ArrayList<>();
noteAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, noteList);
noteListView.setAdapter(noteAdapter);
// 保存按钮点击事件
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取用户输入的笔记内容
String note = noteEditText.getText().toString();
// 将笔记内容保存到本地存储中
SharedPreferences sharedPreferences = getSharedPreferences("note", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("note_" + System.currentTimeMillis(), note);
editor.apply();
// 将笔记内容添加到笔记列表中
noteList.add(note);
noteAdapter.notifyDataSetChanged();
// 清空输入框
noteEditText.setText("");
}
});
// 笔记列表长按事件
noteListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// 获取用户长按的笔记内容
String note = noteList.get(position);
// 弹出对话框询问用户是否要删除该笔记
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setMessage("确定要删除该笔记吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 从本地存储中删除该笔记
SharedPreferences sharedPreferences = getSharedPreferences("note", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
for (Map.Entry<String, ?> entry : sharedPreferences.getAll().entrySet()) {
if (entry.getValue().equals(note)) {
editor.remove(entry.getKey());
editor.apply();
break;
}
}
// 从笔记列表中删除该笔记
noteList.remove(position);
noteAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
return true;
}
});
// 加载本地存储中已保存的笔记列表
SharedPreferences sharedPreferences = getSharedPreferences("note", MODE_PRIVATE);
for (Map.Entry<String, ?> entry : sharedPreferences.getAll().entrySet()) {
noteList.add(entry.getValue().toString());
}
noteAdapter.notifyDataSetChanged();
}
}
```
以上代码示例中,实现了笔记的保存、编辑和删除功能,并通过SharedPreferences来实现笔记的本地存储。
阅读全文