Android Notepad project development

Wrote an Android Notepad applet, now record it.

Considering that it is a notepad applet, the recorded content is only text, and the content will not be too long, so we choose to use SQLite database and store the data on the user's mobile phone. If the database is involved, it is naturally an entity. First design the entity data table: dbhelper.java

After creating the data table, you naturally need to operate the database and crud data. I encapsulate all operations related to the database: notedb.java

After designing the database, you need an entity class corresponding to the database: note.java

Next, design the app home page: main_ activity.xml

The specific effects are as follows (the icon is too lazy to change the color):

On the left is a button about app, and on the right is the button of new Notepad.

Because the home page needs to display the recorded content, I choose to use listview to display it. When listview is used, it corresponds to a data source and an adapter. So I designed a style for each sub item to show the creation or update time on the left and the content on the right. As follows: List_ item.xml

After creating the listview, prepare the adapter for it: myadapter. Java

Here, the viewholder is used to make the listview scroll without having to recreate the object every time, so as to improve the performance.

After creating the listview and preparing the adapter, you need to prepare the data source for the listview, which is to be read from the database. However, database operations and network access are time-consuming operations. If the main UI thread is used to perform response operations, anr may occur. Therefore, I use asynctask to perform database operations here. The main activity code is as follows: mainactivity.java

In the above code, I created a new newasynctask class to inherit asynctask and perform the operation of reading data from the database. In the onpostexecute() method, I updated the UI, such as displaying the data in the listview. There are several pieces of data at the bottom of the following page. Also, if I consider creating a new Notepad, it is another activity. When returning from the other Activity to the main Activity, the main page should be refreshed once more, refreshing the data and displaying, so I called the NewAsyncTask ().Execute () method in the onResume () method, and refreshed the display when the activity resumed. Next is the activity of the new Notepad. The layout is as follows: write_ note.xml

The specific effects are as follows:

The activity of the new Notepad is as follows: addnoteactivity.java

Creating a new notepad and inserting data into the database are time-consuming operations like reading data from the database, so I still use asynctask. In the onpreexecute() method, first obtain the current time of the system, format it, store it, save the input text, and then doinbackground() to save the data. Here, I consider that the user entered the content but did not save it. Judgment is added to the processing events of the return key at the top or the return key of the system. If the text is empty and the space is also empty, you will not save and exit the current activity directly. If there is content, a dialog box will pop up to prompt you whether to save. If yes, you will save. Otherwise, you will exit the current activity without saving.

The next step is to view or modify the layout of a notepad. I directly reuse the layout of the new Notepad. Because there's no difference --

The next step is to view or modify the activity of a notepad. Before, I wanted to click a notepad to enter the Notepad, display the Notepad directly on the page, and the user can edit it directly at the end of the content. Therefore, a sub item click event is required here. I have already written in mainactivity to get the object of the currently clicked item first. I spent a lot of time here. I don't know how to read the object of this item when clicking this item. Finally, I checked the source code and API. I saw that AdapterView in the parameter is a generic type. I tried to start with it, forcibly convert it into a note object, and then try to get the ID. unexpectedly, it became it. -- Therefore, I get the ID of the note object in the currently clicked item, put it in intent, and start the activity with this parameter. Here, the activity of viewing or modifying a notepad officially begins, as follows: updateorreadactivity.java

Asynctask is still used to operate the database. Here, I consider whether there is any change. Use a variable to store the original data. Judge whether there is any change when the user clicks the return at the top or the system return key. If so, prompt the user whether to save the change. If the content is modified and there are no words, the Notepad will be deleted automatically. Because the operation of deleting Notepad still needs to be used on the home page, I put it forward as a separate class, which is no longer an internal class. As follows:

Next is the last item of crud. When deleting data, I designed to click to enter this Notepad to view or modify this Notepad, and then I thought about long-term deletion. Long press to pop up a dialog box, prompting whether to delete. If yes, delete. Otherwise, nothing will be done. Therefore, you can see the listener of long press event in mainactivity. However, because of Android's event distribution mechanism, long press events will trigger click events. Therefore, you need to set such a property in the listview to listen to click events and long press events at the same time. android:descendantFocusability="blocksDescendants"

The main functions are almost completed. The next step is to optimize the app. I designed the transition animation, the guide page, and whether to start the app for the first time. Yes, the transition animation transitions to the guide page, and then to the home page. Otherwise, the transition animation will directly enter the home page after the transition. The switching animation of the guide page is also designed, and nineol dandroid is used to ensure that the animation can be displayed on low version mobile phones.

In the part of APP optimization, you can see my other blog, portal: Android realizes transition animation and guide page. Android judges whether to start the app for the first time

The project address is: https://github.com/someonexiaole/Android Notepad is.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>