在學習了郭神的第一行代碼前半段之后,想通過一次實踐來完成對已學知識的鞏固。於是碼下了這個圖書管理系統客戶端。
IDE Android studio,語言 JAVA、XML;
在剛開始設計的時候對於這個程序的設定是:
1.支持學生、教師、工作人員三種不同身份的客戶。
2.學生、老師各自注冊賬號,並用賬號登錄。
3.學生、老師能夠查詢、借閱、歸還圖書,逾期沒有歸還圖書的客戶將被列入黑名單。
4.含有圖書館的介紹與新聞通知板塊。
5.管理人員能夠添加書籍。
6.含有新書上架和借閱排行榜兩個list。
7.所有信息從服務器上訪問得到,支持不同客戶端共享相同數據
在經過了一段時間的磨蹭之后,終於將圖書管理系統基本完成:
但和最開始的設定有了一些偏差:由於教師和學生的不同之處僅僅是借閱書籍的最大數量不同,因此將教師這一身份舍去。在寫客戶端向服務器(Windows下建了一個微型服務器)通信的時候遇到了很大的困難,長時間的碼代碼、編譯、通過、服務器無反應之后,暫時放棄了連接服務器這一功能,將所有用戶與書籍的數據儲存在了Android系統自帶的數據庫中。這樣使得過程難度降低了很多,但是無法實現不同客戶端訪問的數據統一化,也就是從聯網退化至了單機。
但是學習仍在路上,以后有時間當將這些不足得以改善。
下面來講解圖書管理系統的制作過程。
主頁面:


新手上路,界面很low》》》》》》

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:gravity="center" 8 android:orientation="vertical" 9 tools:context=".MainActivity"> 10 11 <ImageView 12 android:id="@+id/image_view_id" 13 android:layout_width="match_parent" 14 android:layout_height="0dp" 15 android:layout_weight="6" 16 android:src="@drawable/img_1" /> 17 18 <TextView 19 android:layout_width="match_parent" 20 android:layout_height="0dp" 21 android:gravity="center" 22 android:textSize="30sp" 23 android:layout_weight="2" 24 android:text="歡迎使用圖書館安卓客戶端" /> 25 26 <LinearLayout 27 android:layout_width="match_parent" 28 android:layout_height="0dp" 29 android:layout_weight="4" 30 android:orientation="horizontal"> 31 32 <Button 33 android:id="@+id/introduce_id" 34 android:layout_width="0dp" 35 android:layout_height="match_parent" 36 android:layout_weight="1" 37 android:textSize="21sp" 38 android:text="圖書館介紹" /> 39 40 <Button 41 android:id="@+id/news_id" 42 android:layout_width="0dp" 43 android:layout_height="match_parent" 44 android:layout_weight="1" 45 android:textSize="21sp" 46 android:text="圖書館新聞通知" /> 47 </LinearLayout> 48 49 <LinearLayout 50 android:layout_width="match_parent" 51 android:layout_height="0dp" 52 android:layout_weight="4" 53 android:orientation="horizontal"> 54 55 <Button 56 android:id="@+id/newBook_id" 57 android:layout_width="0dp" 58 android:layout_height="match_parent" 59 android:layout_weight="1" 60 android:textSize="21sp" 61 android:text="新書上架" /> 62 63 <Button 64 android:id="@+id/ranking_id" 65 android:layout_width="0dp" 66 android:layout_height="match_parent" 67 android:layout_weight="1" 68 android:textSize="21sp" 69 android:text="借閱排行榜" /> 70 </LinearLayout> 71 72 <LinearLayout 73 android:layout_width="match_parent" 74 android:layout_height="0dp" 75 android:layout_weight="4"> 76 77 <Button 78 android:id="@+id/registerStudent_id" 79 android:layout_width="0dp" 80 android:layout_height="match_parent" 81 android:layout_weight="1" 82 android:textSize="21sp" 83 android:text="學生注冊" /> 84 <Button 85 android:id="@+id/blacklist_id" 86 android:layout_width="0dp" 87 android:layout_height="match_parent" 88 android:layout_weight="1" 89 android:textSize="21sp" 90 android:text="黑名單" /> 91 </LinearLayout> 92 </LinearLayout>
下面是主程序的代碼:

1 package com.example.administrator.library1; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.widget.Button; 10 import android.widget.Toast; 11 12 import org.litepal.LitePal; 13 /*------ 14 郭志---18年暑假 15 */ 16 17 public class MainActivity extends AppCompatActivity { 18 19 private Button button_introduce,button_information,button_ranking,button_newBooks,button_register_student,button_blacklist; 20 @Override 21 public boolean onCreateOptionsMenu(Menu menu) { 22 getMenuInflater().inflate(R.menu.main,menu); 23 return true; 24 } 25 26 @Override 27 public boolean onOptionsItemSelected(MenuItem item) { 28 switch (item.getItemId()) 29 { 30 case R.id.student_id: 31 Intent intent_student=new Intent(MainActivity.this,Login_student.class); 32 startActivity(intent_student); 33 break; 34 /*case R.id.teacher_id: 35 Intent intent_teacher=new Intent(MainActivity.this,Login_teacher.class); 36 startActivity(intent_teacher); 37 break;*/ 38 case R.id.staff_id: 39 Intent intent_staff=new Intent(MainActivity.this,Login_staff.class); 40 startActivity(intent_staff); 41 break; 42 } 43 return true; 44 } 45 46 @Override 47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 setContentView(R.layout.activity_main); 50 button_introduce=findViewById(R.id.introduce_id); 51 button_register_student=findViewById(R.id.registerStudent_id); 52 button_blacklist=findViewById(R.id.blacklist_id); 53 button_information=findViewById(R.id.news_id); 54 button_ranking=findViewById(R.id.ranking_id); 55 button_newBooks=findViewById(R.id.newBook_id); 56 57 button_introduce.setOnClickListener(new View.OnClickListener() { 58 @Override 59 public void onClick(View view) { 60 Intent intent=new Intent(MainActivity.this,Introduction.class); 61 startActivity(intent); 62 finish(); 63 } 64 }); 65 button_register_student.setOnClickListener(new View.OnClickListener() { 66 @Override 67 public void onClick(View view) { 68 Intent intent=new Intent(MainActivity.this,Register_student.class); 69 startActivity(intent); 70 finish(); 71 } 72 }); 73 button_blacklist.setOnClickListener(new View.OnClickListener() { 74 @Override 75 public void onClick(View view) { 76 Intent intent=new Intent(MainActivity.this,Blacklist.class); 77 startActivity(intent); 78 finish(); 79 } 80 }); 81 button_information.setOnClickListener(new View.OnClickListener() { 82 @Override 83 public void onClick(View view) { 84 Intent intent=new Intent(MainActivity.this,News.class); 85 startActivity(intent); 86 finish(); 87 } 88 }); 89 button_ranking.setOnClickListener(new View.OnClickListener() { 90 @Override 91 public void onClick(View view) { 92 Intent intent=new Intent(MainActivity.this,Ranking.class); 93 startActivity(intent); 94 finish(); 95 } 96 }); 97 button_newBooks.setOnClickListener(new View.OnClickListener() { 98 @Override 99 public void onClick(View view) { 100 Intent intent=new Intent(MainActivity.this,New_Books.class); 101 startActivity(intent); 102 finish(); 103 } 104 }); 105 106 } 107 }
當然我們的程序都得先在注冊表中進行注冊,一些權限的開啟也在注冊表中。

1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.library1"> 4 5 <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 6 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 7 <uses-permission android:name="android.permission.INTERNET" /> 8 9 <application 10 android:name="org.litepal.LitePalApplication" 11 android:allowBackup="true" 12 android:icon="@mipmap/ic_launcher" 13 android:label="圖書館" 14 android:roundIcon="@mipmap/ic_launcher_round" 15 android:supportsRtl="true" 16 android:theme="@style/AppTheme"> 17 <activity android:name=".MainActivity"> 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 <activity android:name=".Login_student" /> 25 <activity android:name=".Login_teacher" /> 26 <activity android:name=".Login_staff" /> 27 <activity android:name=".Register_student" /> 28 <activity android:name=".Register_teacher" /> 29 <activity android:name=".Introduction" /> 30 <activity android:name=".Student_homepage" /> 31 <activity android:name=".Teacher_homepage" /> 32 <activity android:name=".Staff_homepage" /> 33 <activity android:name=".News" /> 34 <activity android:name=".staff_input" /> 35 <activity android:name=".Ranking" /> 36 <activity android:name=".New_Books" /> 37 <activity android:name=".Blacklist"></activity> 38 </application> 39 40 </manifest>
整個項目中用到了Github上面的許多開源項目,如Litepal,OKhttp,這些都在build grade中引用過,代碼貼出:

1 apply plugin: 'com.android.application' 2 3 android { 4 compileSdkVersion 26 5 defaultConfig { 6 applicationId "com.example.administrator.library1" 7 minSdkVersion 15 8 targetSdkVersion 26 9 versionCode 1 10 versionName "1.0" 11 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 } 13 buildTypes { 14 release { 15 minifyEnabled false 16 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 } 18 } 19 } 20 21 dependencies { 22 implementation fileTree(dir: 'libs', include: ['*.jar']) 23 implementation 'com.android.support:appcompat-v7:26.1.0' 24 implementation 'com.android.support.constraint:constraint-layout:1.1.2' 25 testImplementation 'junit:junit:4.12' 26 implementation 'com.squareup.okhttp3:okhttp:3.11.0' 27 implementation 'com.android.support:recyclerview-v7:26.1.0' 28 implementation 'org.litepal.android:core:2.0.0' 29 androidTestImplementation 'com.android.support.test:runner:1.0.2' 30 androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 31 }
整個管理系統無論是哪個用戶,哪一個功能都離不開數據,這些數據都存儲在本地的數據庫里。關於數據庫的設計是這樣的:
1 <?xml version="1.0" encoding="utf-8"?> 2 <litepal> 3 <dbname value="library"></dbname> 4 <version value="10"></version> 5 <list> 6 <mapping class="com.example.administrator.library1.Student"></mapping> 7 <mapping class="com.example.administrator.library1.Teacher"></mapping> 8 <mapping class="com.example.administrator.library1.Book"></mapping> 9 <mapping class="com.example.administrator.library1.Staff"></mapping> 10 <mapping class="com.example.administrator.library1.Simple"></mapping> 11 </list> 12 </litepal>
library庫由五個表構成:學生、教師(停用)、書籍、職員、簡單信息(用於存儲記住密碼之后的賬號、密碼信息)組成。
下一篇,我將來講述學生的注冊、登錄、借還書功能的實現。
(先附上整個項目的源碼地址:https://github.com/Guozhi-explore/androidlibrary)