使用AndroidStudio完成一个简易备忘录功能,主页点击加号按钮,弹出可编辑对话框,点击保存之后动态在该布局加入数据并使用sharedpreferencs保存在该页面,动态增长该布局的长度;点击减号删除该信息,以上功能都在主界面成,请写出示例代码

时间: 2024-02-11 08:10:00 浏览: 20
好的,下面是一个简单的实现备忘录功能的示例代码,供您参考: 1. 主界面布局文件 activity_main.xml: ```xml <LinearLayout android:id="@+id/notes_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 加号按钮 --> <Button android:id="@+id/add_note_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:textSize="20sp" /> </LinearLayout> ``` 2. 备忘录信息布局文件 item_note.xml: ```xml <LinearLayout android:id="@+id/note_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp"> <TextView android:id="@+id/note_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="18sp" /> <!-- 减号按钮 --> <Button android:id="@+id/delete_note_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="20sp" /> </LinearLayout> ``` 3. 主界面对应的 Activity 类 MainActivity.java: ```java public class MainActivity extends AppCompatActivity { private LinearLayout notesLayout; private Button addNoteBtn; private SharedPreferences sharedPreferences; private int notesCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); notesLayout = findViewById(R.id.notes_layout); addNoteBtn = findViewById(R.id.add_note_btn); // 获取保存备忘录信息的 SharedPreferences 对象 sharedPreferences = getSharedPreferences("notes", MODE_PRIVATE); // 获取当前已有的备忘录数量 notesCount = sharedPreferences.getInt("notes_count", 0); // 加载已保存的备忘录信息 for (int i = 0; i < notesCount; i++) { String note = sharedPreferences.getString("note_" + i, ""); addNoteView(note); } // 点击加号按钮弹出对话框 addNoteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showAddNoteDialog(); } }); } // 弹出添加备忘录对话框 private void showAddNoteDialog() { final EditText editText = new EditText(this); new AlertDialog.Builder(this) .setTitle("添加备忘录") .setView(editText) .setPositiveButton("保存", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String note = editText.getText().toString().trim(); if (!TextUtils.isEmpty(note)) { addNoteView(note); saveNoteToSharedPreferences(note); } } }) .setNegativeButton("取消", null) .show(); } // 动态添加备忘录信息到界面 private void addNoteView(String note) { View noteView = getLayoutInflater().inflate(R.layout.item_note, null); TextView noteText = noteView.findViewById(R.id.note_text); Button deleteNoteBtn = noteView.findViewById(R.id.delete_note_btn); noteText.setText(note); // 点击减号按钮删除备忘录信息 deleteNoteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { notesLayout.removeView(noteView); removeNoteFromSharedPreferences(note); } }); notesLayout.addView(noteView); } // 将备忘录信息保存到 SharedPreferences 中 private void saveNoteToSharedPreferences(String note) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("note_" + notesCount, note); editor.putInt("notes_count", ++notesCount); editor.apply(); } // 从 SharedPreferences 中删除备忘录信息 private void removeNoteFromSharedPreferences(String note) { SharedPreferences.Editor editor = sharedPreferences.edit(); for (int i = 0; i < notesCount; i++) { if (note.equals(sharedPreferences.getString("note_" + i, ""))) { editor.remove("note_" + i); break; } } editor.putInt("notes_count", --notesCount); editor.apply(); } } ``` 以上代码实现了一个简单的备忘录功能,包括添加、删除备忘录信息,动态增加和删除备忘录布局等。需要注意的是,此示例代码只是一个简单的实现,还可以进行优化和改进,如添加数据校验、添加滑动删除功能等。

相关推荐

最新推荐

recommend-type

android实现点击按钮切换不同的fragment布局

主要为大家详细介绍了android实现点击按钮切换不同的fragment布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

详解Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限

项目中为了实现账号多设备登录的监听 一个账号在别的设备登录时在该设备上需要弹出对话框提示 故而用到全局对话框 方案一、 1、在开发中有时会用到全局弹出对话框但必须在manifest中申请权限: &lt;uses android:name...
recommend-type

Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法

主要介绍了Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法,需要的朋友可以参考下
recommend-type

Android悬浮窗按钮实现点击并显示/隐藏多功能列表

主要为大家详细介绍了Android悬浮窗按钮实现点击并显示/隐藏多功能列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Android Studio实现简易计算器(表格布局TableLayout)

主要为大家详细介绍了Android Studio实现简易计算器,表格布局TableLayout,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。