搭建一个Android记事本,代码中包含新建,保存,查找,替换这几项功能,区分java和xml

时间: 2023-12-07 10:02:14 浏览: 34
好的,搭建一个 Android 记事本需要分为前端和后端两个部分,其中前端主要使用 XML 和 Java 语言进行开发,后端主要使用 Java 语言进行开发。 首先,我们来介绍前端部分的代码: 1. XML 布局文件 在 res/layout 文件夹下创建一个名为 activity_main.xml 的布局文件,用于设置记事本的 UI 界面,代码如下: ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomBar" android:layout_alignParentTop="true" android:background="@android:color/white" android:gravity="top|left" android:inputType="textMultiLine" android:padding="10dp" android:scrollbars="vertical" /> <LinearLayout android:id="@+id/bottomBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/newButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="新建" /> <Button android:id="@+id/saveButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="保存" /> <Button android:id="@+id/findButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="查找" /> <Button android:id="@+id/replaceButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="替换" /> </LinearLayout> </RelativeLayout> ``` 2. Java 代码文件 在 app/java/com.example.notepad 文件夹下创建一个名为 MainActivity.java 的 Java 代码文件,用于实现记事本的各项功能,代码如下: ```java package com.example.notepad; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; 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.InputStreamReader; public class MainActivity extends AppCompatActivity { EditText editText; Button newButton, saveButton, findButton, replaceButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.editText); newButton = findViewById(R.id.newButton); saveButton = findViewById(R.id.saveButton); findButton = findViewById(R.id.findButton); replaceButton = findViewById(R.id.replaceButton); // 新建按钮点击事件 newButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.setText(""); } }); // 保存按钮点击事件 saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); if (text.isEmpty()) { Toast.makeText(MainActivity.this, "请输入内容", Toast.LENGTH_SHORT).show(); } else { try { FileOutputStream fos = openFileOutput("note.txt", MODE_PRIVATE); fos.write(text.getBytes()); fos.close(); Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } } }); // 查找按钮点击事件 findButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "查找功能正在开发中", Toast.LENGTH_SHORT).show(); } }); // 替换按钮点击事件 replaceButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "替换功能正在开发中", Toast.LENGTH_SHORT).show(); } }); // 读取文件中的内容 try { FileInputStream fis = openFileInput("note.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } fis.close(); editText.setText(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上就是前端部分的代码。接下来,我们来介绍后端部分的代码: 1. Java 代码文件 在 app/java/com.example.notepad 文件夹下创建一个名为 Note.java 的 Java 代码文件,用于封装 Note 类,代码如下: ```java package com.example.notepad; public class Note { private String title; private String content; public Note(String title, String content) { this.title = title; this.content = content; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } ``` 2. 数据库帮助类文件 在 app/java/com.example.notepad 文件夹下创建一个名为 NoteDBHelper.java 的 Java 代码文件,用于封装数据库帮助类,代码如下: ```java package com.example.notepad; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class NoteDBHelper extends SQLiteOpenHelper { private static final int DB_VERSION = 1; private static final String DB_NAME = "Note.db"; public static final String NOTE_TABLE_NAME = "note"; public static final String NOTE_COLUMN_ID = "_id"; public static final String NOTE_COLUMN_TITLE = "title"; public static final String NOTE_COLUMN_CONTENT = "content"; public NoteDBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String createTable = "CREATE TABLE " + NOTE_TABLE_NAME + "(" + NOTE_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_COLUMN_TITLE + " TEXT, " + NOTE_COLUMN_CONTENT + " TEXT);"; db.execSQL(createTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + NOTE_TABLE_NAME); onCreate(db); } } ``` 3. 数据库操作类文件 在 app/java/com.example.notepad 文件夹下创建一个名为 NoteDAO.java 的 Java 代码文件,用于封装数据库操作类,代码如下: ```java package com.example.notepad; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; public class NoteDAO { private NoteDBHelper dbHelper; public NoteDAO(Context context) { dbHelper = new NoteDBHelper(context); } public void add(Note note) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(NoteDBHelper.NOTE_COLUMN_TITLE, note.getTitle()); values.put(NoteDBHelper.NOTE_COLUMN_CONTENT, note.getContent()); db.insert(NoteDBHelper.NOTE_TABLE_NAME, null, values); db.close(); } public void delete(int id) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.delete(NoteDBHelper.NOTE_TABLE_NAME, NoteDBHelper.NOTE_COLUMN_ID + "=?", new String[]{String.valueOf(id)}); db.close(); } public void update(Note note) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(NoteDBHelper.NOTE_COLUMN_TITLE, note.getTitle()); values.put(NoteDBHelper.NOTE_COLUMN_CONTENT, note.getContent()); db.update(NoteDBHelper.NOTE_TABLE_NAME, values, NoteDBHelper.NOTE_COLUMN_ID + "=?", new String[]{String.valueOf(note.getId())}); db.close(); } public List<Note> getAll() { List<Note> list = new ArrayList<>(); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query(NoteDBHelper.NOTE_TABLE_NAME, null, null, null, null, null, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(NoteDBHelper.NOTE_COLUMN_ID)); String title = cursor.getString(cursor.getColumnIndex(NoteDBHelper.NOTE_COLUMN_TITLE)); String content = cursor.getString(cursor.getColumnIndex(NoteDBHelper.NOTE_COLUMN_CONTENT)); Note note = new Note(); note.setId(id); note.setTitle(title); note.setContent(content); list.add(note); } cursor.close(); db.close(); return list; } } ``` 以上就是后端部分的代码。需要注意的是,这里只是示例代码,实际开发中需要根据具体需求进行适当的调整和改进。

相关推荐

最新推荐

recommend-type

记事本的设计与功能实现 Java课程设计报告 源代码

主要用到的是图形界面,可以在文本编辑框里进行编辑,在菜单选项里进行打开、保存、另存为、查找、替换、等功能。界面上的按钮有文件、编辑、工具和帮助。还添加有工具条,工具条中有按钮复制、粘贴和全选。
recommend-type

Scrapy-1.8.2.tar.gz

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
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的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

导入numpy库,创建两个包含9个随机数的3*3的矩阵,将两个矩阵分别打印出来,计算两个数组的点积并打印出来。(random.randn()、dot()函数)

可以的,以下是代码实现: ```python import numpy as np # 创建两个包含9个随机数的3*3的矩阵 matrix1 = np.random.randn(3, 3) matrix2 = np.random.randn(3, 3) # 打印两个矩阵 print("Matrix 1:\n", matrix1) print("Matrix 2:\n", matrix2) # 计算两个数组的点积并打印出来 dot_product = np.dot(matrix1, matrix2) print("Dot product:\n", dot_product) ``` 希望