In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to realize the notepad function of Android mobile phone development and design". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the notepad function of Android mobile phone development and design" can help you solve the problem.
I. demand analysis
1.1 Business requirements Analysis
In recent years, with the acceleration of the pace of life, the dual pressure of work and life is invading people in an all-round way, so it is very important to avoid the adverse consequences caused by forgetting many things in work and life. To this end, we develop a simple notepad based on Android system, which can easily record life and work to many things, so as to help people manage their time effectively.
1.2 functional requirements analysis
This notepad project hopes to develop a simple application in line with users' living and working habits, which can meet the needs of users in all aspects, and can add, view, modify and delete notes. requires a rich function and a good user interface and interactive experience.
II. Project design
2.1 functional module design
2.1.1 basic operation of notepad
The basic operation of notepad is the core part of the project, which provides the function of adding, viewing, modifying and deleting notepad information, and provides the function of dynamic update of notepad.
2.1.2 notepad main interface list display
Users may need to create a lot of notes, and need the basic operation of these records process. The list display of notepad can make the user interface more concise and clear, and bring convenience to users.
2.1.3 notepad data storage
The most important function of notepad is to record and save dates and events that are easy to be forgotten by users. in order to keep the information of user records permanently, the information needs to be stored in the database. The information fields that notepad needs to save are the number, the content of the event and the specific time when the event was saved.
Its functional module diagram is shown in the figure.
2.2 Database design
From the analysis of the above functional modules, the database design of this notepad project mainly includes three field names: number id, event content content and time notetime when the event was saved. The database table is shown in the following table:
2.3 Interface design
2.3.1 notepad main interface
The interface mainly includes adding buttons and a list of records. The notepad main interface is designed as shown in the following figure.
2.3.2 add Records Interface
The interface mainly includes clearing content and saving content buttons and text editing. Add notes in notepad as shown in the figure below.
III. Project realization
3.1 NotepadBean Class
Since each record in notepad has its unique number id, record content notepadContent, and time notepadTime attribute when the record was saved, we need to create a NotepadBean class to hold these attributes and implement its corresponding getter and setter methods, the main code is as follows:
Public class NotepadBean {private String id;// record number the content of the private String notepadContent;// record the time private String notepadTime;// saved the record public String getId () {return id;} public void setId (String id) {this.id = id;} public String getNotepadContent () {return notepadContent;} public void setNotepadContent (String notepadContent) {this.notepadContent = notepadContent } public String getNotepadTime () {return notepadTime;} public void setNotepadTime (String notepadTime) {this.notepadTime = notepadTime;}}
3.2 NotepadAdapter classes
Since the list of records in notepad interface is displayed using ListView controls, you need to create a data adapter NotepadAdapter class to adapt data to ListView controls. We can first create a NotepadAdapter class, and then create a ViewHolder class in the NotepadAdapter class to initialize the controls in the Item interface. The main code is as follows:
Public View getView (int position, View convertView, ViewGroup parent) {ViewHolder viewHolder; if (convertView==null) {/ / load the layout file convertView=layoutInflater.inflate corresponding to the Item interface (R.layout.notepadsignitem.null); viewHolder=new ViewHolder (convertView); / / create ViewHolder object convertView.setTag (viewHolder); / / create ViewHolder object} else {viewHolder= (ViewHolder) convertView.getTag () / / convertView associate ViewHolder object} NotepadBean notepadBean= (NotepadBean) getItem (position); / / display the acquired data to the corresponding control viewHolder.tvNotepadContent.setText (notepadBean.getNotepadContent ()); viewHolder.tvNotepadTime.setText (notepadBean.getNotepadTime ()); return convertView;} class ViewHolder {TextView tvNotepadContent; TextView tvNotepadTime; public ViewHolder (View view) {tvNotepadContent=view.findViewById (R.id.item_content) / / the content of the record tvNotepadTime=view.findViewById (R.id.item_time); / / the time when the record was saved}}
3.3CLA SQLiteHelper
The data recorded in the notepad program is stored and read by operating the database. We need to create a SQLiteHelper class to add, delete, modify and query the tables in the database, and use the tool class DBUtils in the database to define the database name, table name, database version, column name in the database table, and obtain the current date and other information. The main code is as follows:
Create the database:
Public SQLiteHelper (Context context) {super (context, DBUtils.DATABASE_NAME,null,DBUtils.DATABASE_VERION); sqLiteDatabase=this.getWritableDatabase ();}
Create a table:
@ Overridepublic void onCreate (SQLiteDatabase db) {db.execSQL ("CREATE TABLE" + DBUtils.DATABASE_TABLE+ "(" + DBUtils.NOTEPAD_ID+ "INTEGER PRIMARY KEY AUTOINCREMENT," + DBUtils.NOTEPAD_CONTENT+ "text," + DBUtils.NOTEPAD_TIME+ "text)");}
Add data:
Public boolean insertData (String userContent,String userTime) {ContentValues contentValues=new ContentValues (); contentValues.put (DBUtils.NOTEPAD_CONTENT,userContent); contentValues.put (DBUtils.NOTEPAD_TIME,userTime); return sqLiteDatabase.insert (DBUtils.DATABASE_TABLE,null,contentValues) > 0;}
Delete data:
Public boolean deleteData (String id) {String sql=DBUtils.NOTEPAD_ID+ "=?"; String [] contentValuesArray=new String [] {String.valueOf (id)}; return sqLiteDatabase.delete (DBUtils.DATABASE_TABLE,sql,contentValuesArray) > 0;}
Modify the data:
Public boolean updateData (String id,String content,String userYear) {ContentValues contentValues=new ContentValues (); contentValues.put (DBUtils.NOTEPAD_CONTENT,content); contentValues.put (DBUtils.NOTEPAD_TIME,userYear); String sql=DBUtils.NOTEPAD_ID+ "=?"; String [] strings=new String [] {id}; return sqLiteDatabase.update (DBUtils.DATABASE_TABLE,contentValues,sql,strings) > 0;}
Query data:
Public List query () {Listlist=new ArrayList (); Cursor cursor=sqLiteDatabase.query (DBUtils.DATABASE_TABLE,null,null, null,null,null,DBUtils.NOTEPAD_ID+ "desc"); if (cursorily ordered null) {while (cursor.moveToNext ()) {NotepadBean noteInfo=new NotepadBean (); String id=String.valueOf (cursor.getInt (cursor.getColumnIndex (DBUtils.NOTEPAD_ID) String content=cursor.getString (cursor.getColumnIndex (DBUtils.NOTEPAD_CONTENT)); String time=cursor.getString (cursor.getColumnIndex (DBUtils.NOTEPAD_TIME)); noteInfo.setId (id); noteInfo.setNotepadContent (content); noteInfo.setNotepadTime (time); list.add (noteInfo);} cursor.close ();} return list;}
3.4 NotepadActivity class
The notepad main interface includes the functions of display list and add button. We create the NotepadActivity class implementation, in which the display list is implemented in the NotepadActivity class by creating a showQueryData () method to query the record information stored in the database and display the information in the record list. In fact, the code is as follows:
Private void showQueryData () {if (listings null) {list.clear ();} list=mSQLiteHelper.query (); adapter=new NotepadAdapter (this,list); listView.setAdapter (adapter);}
Set the click event for "add button" through the setOnClickListener () method. When the button is clicked, it jumps to the interface of adding records. In fact, the code is as follows:
Protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); listView=findViewById (R.id.listview); ImageView imageView=findViewById (R.id.add); initData (); imageView.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent=new Intent (NotepadActivity.this,RecordActivity.class); startActivityForResult (intent,1)) });}
3.5 RecordActivity classes
RecordActivity is to modify the record. We monitor the click event of the Item through the setOnItemClickListener () method of listView in NotepadActivity, and jump to RecordActivity with the record content of the clicked Item, which will display the content of the record according to the obtained data. In addition, when we need to delete a record in the notepad list, we need to long press Item in the list, and a dialog box will pop up to prompt whether to delete the event corresponding to Item. The code for the setOnItemClickListener () method is as follows:
ListView.setOnItemClickListener (new AdapterView.OnItemClickListener () {@ Override public void onItemClick (AdapterViewparent,View view,int position,long id) {NotepadBean notepadBean=list.get (position); Intent intent=new Intent (NotepadActivity.this,RecordActivity.class); intent.putExtra ("id", notepadBean.getId ()); intent.putExtra ("content", notepadBean.getNotepadContent ()); intent.putExtra ("time", notepadBean.getNotepadTime ()) NotepadActivity.this.startActivityForResult (intent,1);}}); listView.setOnItemLongClickListener (new AdapterView.OnItemLongClickListener () {@ Override public boolean onItemLongClick (AdapterView parent, View view, final int position, long id) {AlertDialog dialog AlertDialog.Builder builder=new AlertDialog.Builder (NotepadActivity.this) .setMessage ("delete this record?") .setPositiveButton ("OK", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {NotepadBean notepadBean=list.get (position)) If (mSQLiteHelper.deleteData (notepadBean.getId () {list.remove (position); / / delete the corresponding Item adapter.notifyDataSetChanged (); / / update the notepad page Toast.makeText (NotepadActivity.this, "deleted successfully", Toast.LENGTH_LONG) .show () ) .setNegativeButton ("cancel", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {dialog.dismiss () }}); dialog=builder.create (); dialog.show (); return true;}})
RecordActivity uses the initData () function to receive the passed data, and the code is as follows:
Public void initData () {mSQLiteHelper=new SQLiteHelper (this); noteName.setText ("add record"); Intent intent=getIntent (); if (intent record null) {id=intent.getStringExtra ("id"); if (idled record null) {noteName.setText ("modify record"); content.setText (intent.getStringExtra ("content")); note_time.setText (intent.getStringExtra ("time")) Note_time.setVisibility (View.VISIBLE);}
At the same time, we can use switch in RecordActivity. The case structure realizes the functions of editing records, saving and clearing edited records, that is, realizing the editing function of records through EditText control, setting click events for the save button; adding the contents and saving time of records to the database through the insertData () method of SQLiteHelper class when clicking the save button; setting click events for the clear button, setting the contents of EditText control to an empty string through the setText () method when clicking the clear button. The OnClick () function code is as follows:
Public void onClick (View v) {switch (v.getId ()) {case R.id.note_back: finish (); break; case R.id.delete: content.setText (""); break; case R.id.note_save: String noteContent = content.getText (). ToString (). Trim () If (identicals null) {/ / modify record function if (noteContent.length () > 0) {if (mSQLiteHelper.updateData (id,noteContent,DBUtils.getTime () {showToast ("modified successfully"); setResult (2); finish () } else {showToast ("modification failed");}} else {showToast ("modified record content cannot be empty") }} else {/ / add record function if (noteContent.length () > 0) {if (mSQLiteHelper.insertData (noteContent,DBUtils.getTime () {showToast ("saved successfully"); setResult (2) Finish ();} else {showToast ("save failed");}} else {showToast ("saved record content cannot be empty");}} break IV. Project testing
1. The main interface of the project operation.
2. Click the main interface to add to the add page, enter "Android course Design" and then click the Save button to return to the main interface and pop up the "Save success" message.
3. Long press the newly created record "Android course Design" will pop up the delete dialog box, click OK to delete, and pop-up "delete successful" message.
4. Select and open the "20182800" record, change it to "20180000", and then click the Save button, which will pop up the "modified successfully" message.
This is the end of the content about "how to realize the notepad function of Android mobile phone development and design". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 235
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.