生活随笔
收集整理的這篇文章主要介紹了
手把手带你完成第一个 android 小程序(快递App)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- (一)簡介
- (二)開始工程
- (1)在github上創建你的代碼庫
- (2)編寫布局文件
- 2.1 MainActivity
- 2.2 TraceAcvitity
- 2.3 ToolBar 的使用
- 2.4 依賴的引入
- (3)項目的代碼實現
- 3.1 代碼的整體理解
- 3.2 MainActivity的代碼實現
- 3.2.1.onCreate
- 3.2.2.按鈕的onCLick事件
- 3.2.3 加載Toolbar 以及 TooBar 的點擊事件
- 3.3 TraceActivity的代碼實現
- 3.3.1 有哪些免費的快遞api?怎樣使用api?
- 3.3.2 Json數據的解析 以及onCreate方法的實現
- 3.3.3 在主線程中更新數據
- 3.3.4 RecycleView 的使用
- (三)將代碼push到github上
- 尾巴:
(一)簡介
- 本文將帶你手把手的實現一個簡單的 android 查快遞小程序 讓你更了解
- android 項目的創建
- github的使用
- JSON數據的解析
以下是程序的截圖(大神勿噴呀)
(二)開始工程
(1)在github上創建你的代碼庫
第一步:創建工程
輸入項目名字 選擇 類型 和 license
在AndroidStudio中創建同名工程
在bit bash 中 進入創建的工程目錄 輸入:
git clone (在github上你的新創建項目的地址)
例如 git clone https://github.com/LQF-dev/DaydayExpress.git
最后push到 github
git add
.git commit -m
"你的commit"git push origin master
(2)編寫布局文件
???注意:布局代碼有點繁瑣,并且本人做的也不夠好,若不按照此布局文件,可選擇直接跳過,查看代碼的實現板塊
2.1 MainActivity
???在主活動中我們會放置兩個 EditText 以及一個按鈕 并且會引入ToolBar 來代替原來的ActionBar
<?xml version
="1.0" encoding
="utf-8"?><androidx
.drawerlayout
.widget
.DrawerLayoutxmlns
:android
="http://schemas.android.com/apk/res/android"xmlns
:app
="http://schemas.android.com/apk/res-auto"android
:id
="@+id/drawer_layout"android
:layout_width
="match_parent"android
:layout_height
="match_parent"><RelativeLayout xmlns
:android
="http://schemas.android.com/apk/res/android"xmlns
:app
="http://schemas.android.com/apk/res-auto"android
:orientation
="vertical"android
:layout_width
="match_parent"android
:layout_height
="match_parent"><androidx
.appcompat
.widget
.Toolbarandroid
:id
="@+id/toolbar"android
:layout_width
="match_parent"android
:layout_height
="?attr/actionBarSize"android
:background
="?attr/colorPrimary"android
:theme
="@style/ThemeOverlay.AppCompat.Dark.ActionBar"app
:popupTheme
="@style/ThemeOverlay.AppCompat.Light"/> ><EditTextandroid
:id
="@+id/edit_expNu"android
:layout_width
="wrap_content"android
:layout_height
="wrap_content"android
:layout_centerInParent
="true"android
:hint
="請輸入快遞單號"/><EditTextandroid
:id
="@+id/edit_expCode"android
:layout_width
="wrap_content"android
:layout_height
="wrap_content"android
:layout_centerInParent
="true"android
:layout_below
="@+id/edit_expNu"android
:hint
="請輸入快遞公司名稱"/><Buttonandroid
:id
="@+id/submit"android
:layout_below
="@+id/edit_expCode"android
:layout_centerInParent
="true"android
:layout_width
="wrap_content"android
:layout_height
="wrap_content"android
:text
="查詢"/></RelativeLayout
><com
.google
.android
.material
.navigation
.NavigationViewandroid
:id
="@+id/nav_view"android
:layout_width
="match_parent"android
:layout_height
="match_parent"android
:layout_gravity
="start"app
:menu
="@menu/nav_menu"app
:headerLayout
="@layout/nav_header"></com
.google
.android
.material
.navigation
.NavigationView
></androidx
.drawerlayout
.widget
.DrawerLayout
>
2.2 TraceAcvitity
- 在此活動中存在
toolbar 顯示標題欄
RelativeLayout 來容納圖片和運送信息
LinearLayout 容納輔助信息
RecycleView 來顯示快遞追蹤信息
<?xml version
="1.0" encoding
="utf-8"?>
<LinearLayout xmlns
:android
="http://schemas.android.com/apk/res/android"xmlns
:app
="http://schemas.android.com/apk/res-auto"xmlns
:tools
="http://schemas.android.com/tools"android
:orientation
="vertical"android
:layout_width
="match_parent"android
:layout_height
="match_parent"><androidx
.appcompat
.widget
.Toolbarandroid
:id
="@+id/toolbar_trace"android
:layout_width
="match_parent"android
:layout_height
="?attr/actionBarSize"android
:background
="?attr/colorPrimary"android
:theme
="@style/ThemeOverlay.AppCompat.Dark.ActionBar"app
:popupTheme
="@style/ThemeOverlay.AppCompat.Light"/> ><RelativeLayoutandroid
:layout_width
="match_parent"android
:layout_height
="wrap_content"android
:layout_marginBottom
="20dp"android
:padding
="20dp"><ImageViewandroid
:id
="@+id/iv_companyImage"android
:layout_width
="wrap_content"android
:layout_height
="wrap_content"android
:layout_alignParentLeft
="true"android
:layout_alignParentTop
="true"android
:src
="@drawable/ic_express_64" /><TextViewandroid
:id
="@+id/text_state"android
:layout_width
="wrap_content"android
:layout_height
="wrap_content"android
:layout_alignParentTop
="true"android
:layout_marginLeft
="44dp"android
:layout_marginTop
="12dp"android
:layout_toRightOf
="@+id/iv_companyImage"android
:text
="正在運送" /><TextViewandroid
:id
="@+id/text_showNumber"android
:layout_width
="wrap_content"android
:layout_height
="wrap_content"android
:layout_below
="@+id/text_state"android
:layout_marginLeft
="44dp"android
:layout_toRightOf
="@+id/iv_companyImage"android
:text
="快遞單號" /></RelativeLayout
><LinearLayoutandroid
:orientation
="horizontal"android
:layout_width
="match_parent"android
:layout_height
="wrap_content"><TextViewandroid
:text
="快速詳情"android
:layout_gravity
="center"android
:layout_width
="0dp"android
:layout_height
="wrap_content"android
:layout_weight
="1"android
:padding
="10dp"/><TextViewandroid
:id
="@+id/tv_time_total"android
:layout_gravity
="center"android
:text
="耗時:"android
:padding
="10dp"android
:layout_width
="0dp"android
:layout_height
="wrap_content"android
:layout_weight
="1"/></LinearLayout
><androidx
.recyclerview
.widget
.RecyclerViewandroid
:id
="@+id/recycle_view_trace"android
:layout_width
="match_parent"android
:layout_height
="wrap_content"></androidx
.recyclerview
.widget
.RecyclerView
></LinearLayout
>
2.3 ToolBar 的使用
???android項目中默認顯示的是ActionBar,故先要在 style.xml 文件中修改項目的主題才能使用Toobar
<resources><!-- Base application theme
. --><style name
="AppTheme" parent
="Theme.AppCompat.Light.NoActionBar"></style
></resources
>
??? 以下是 toobar 的簡單使用 我僅僅在 Toolbar 中增加了一個 掃一掃 的 item,小伙伴可按照你的需求增加不同的功能
<?xml version
="1.0" encoding
="utf-8"?>
<menu xmlns
:android
="http://schemas.android.com/apk/res/android"xmlns
:app
="http://schemas.android.com/apk/res-auto"><itemandroid
:id
="@+id/scan"android
:title
="掃一掃"app
:showAsAction
="never"/>
</menu
>
2.4 依賴的引入
(3)項目的代碼實現
3.1 代碼的整體理解
???當前程序的思路非常簡單
???1. 本地調用查詢快遞 api,發送查詢請求
???2. 接受解析得到的 json 數據,利用GSON或者其他的方式,將有效數據提取出來,
???3. 最后在主線程(UITHread)將數據顯示出來即可
3.2 MainActivity的代碼實現
3.2.1.onCreate
@Overrideprotected void onCreate(Bundle savedInstanceState
) {super.onCreate(savedInstanceState
);setContentView(R
.layout
.activity_main
);Button submit
= (Button
)findViewById(R
.id
.submit
);submit
.setOnClickListener(this);expCode
= (EditText
)findViewById(R
.id
.edit_expCode
);expNu
= (EditText
)findViewById(R
.id
.edit_expNu
);androidx
.appcompat
.widget
.Toolbar toolbar
= (Toolbar
) findViewById(R
.id
.toolbar
);setSupportActionBar(toolbar
);mdrawerLayout
= (DrawerLayout
) findViewById(R
.id
.drawer_layout
) ;ActionBar actionBar
= getSupportActionBar();if(actionBar
!= null
){actionBar
.setDisplayHomeAsUpEnabled(true);actionBar
.setHomeAsUpIndicator(R
.drawable
.ic_menu
);}}
3.2.2.按鈕的onCLick事件
- 通過SharedPreferences 來儲存輸入的數據并且傳遞到 TraceActivity
@Overridepublic void onClick(View v
){switch (v
.getId()){case R
.id
.submit
:final String code
= expCode
.getText().toString();final String num
= expNu
.getText().toString();Log
.d(TAG
, "code = "+ code
);Log
.d(TAG
, "num = " + num
);if(!(code
.equals("STO") || code
.equals("YTO")|| code
.equals("ZTO")) ){Toast
.makeText(this, "僅支持 申通 圓通 中通", Toast
.LENGTH_SHORT
).show();}else{Toast
.makeText(this, "查詢成功", Toast
.LENGTH_SHORT
).show();}SharedPreferences
.Editor editor
= getSharedPreferences("data",MODE_PRIVATE
).edit();editor
.putString("code",code
);editor
.putString("number",num
);editor
.apply();Intent intent
= new Intent(this,TraceActivity
.class);startActivity(intent
);}}
3.2.3 加載Toolbar 以及 TooBar 的點擊事件
public boolean onCreateOptionsMenu(Menu menu
){getMenuInflater().inflate(R
.menu
.toolbar
,menu
);return true;}public boolean onOptionsItemSelected(MenuItem item
){switch (item
.getItemId()){case R
.id
.scan
:Toast
.makeText(this, "你點擊了掃一掃按鈕", Toast
.LENGTH_SHORT
).show();break;case android
.R
.id
.home
:Log
.d(TAG
, "onOptionsItemSelected: open_drawer");mdrawerLayout
.openDrawer(GravityCompat
.START
);break;default:break;}return true;}
3.3 TraceActivity的代碼實現
3.3.1 有哪些免費的快遞api?怎樣使用api?
???可以使用快遞100 快遞鳥的api。 這里我使用的是快遞鳥的免費版(快遞鳥直接注冊即可,快遞100還需要審核)但是。。。快遞鳥免費版本僅僅支持中通 圓通 申通三家的快遞查詢,具體情況可登錄快遞鳥的網站查詢。
??? 當你得到你的id apikey就可以使用快遞鳥的api了,但是如何使用呢?不用著急,我會給您介紹清楚的!
???如下圖所示,打開快遞鳥提供的demo
???并將下載好的.java文件復制到你的項目下面 即可 如圖:
???此接口KdniaoTrackQueryAPI 較為復雜,你不需要了解其中的具體實現,只需要知道存在一個getOrderByJson的方法,該方法需要你傳進去兩個參數(公司編號)(快遞單號)即可返回 JSON 數據
3.3.2 Json數據的解析 以及onCreate方法的實現
???由于我僅僅需要解析出來的物流軌跡,這里就不使用GSON 的解析方法了,而是直接轉化為JSONObject 直接解析
new Thread(new Runnable() {@Overridepublic void run() {try {String respond
= new KdniaoTrackQueryAPI().getOrderTracesByJson(code
,num
);JSONObject jsonObject
= new JSONObject(respond
);JSONArray array
= jsonObject
.getJSONArray("Traces");for (int i
= 0; i
< array
.length(); i
++) {JSONObject object
= array
.getJSONObject(i
);String AcceptStation
= object
.getString("AcceptStation");stations
.add(AcceptStation
);String AcceptTime
= object
.getString("AcceptTime");times
.add(AcceptTime
);} try {Reason
= jsonObject
.getString("Reason");Log
.d(TAG
, "Reason: " + Reason
);} catch (Exception e
) {e
.printStackTrace();}catch (Exception e
) {e
.printStackTrace();}
??? 數據已經得到,并且已經存在 兩個list中,下一步就是更新數據了
3.3.3 在主線程中更新數據
if( Reason
==null
){runOnUiThread(new Runnable() {@Overridepublic void run() {showInfo();}});}else {runOnUiThread(new Runnable() {@Overridepublic void run() {showDefaultInfo();}});}}}).start();
showInfo() 以及 showDefaultInfo()的邏輯請看下面
public void initLoadTrace(){for(int i
=0;i
< times
.size();i
++){LoadTrace loadTraceItem
= new LoadTrace(stations
.get(i
),times
.get(i
));loadTraces
.add(loadTraceItem
);}}public void showTraceInfo(){initLoadTrace();RecyclerView
.LayoutManager layoutManager
= new LinearLayoutManager(this);LoadTraceAdapter adapter
= new LoadTraceAdapter(loadTraces
);recyclerView
.setLayoutManager(layoutManager
);recyclerView
.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration
.VERTICAL
));recyclerView
.setAdapter(adapter
);}public void showInfo(){showTraceInfo();tv_showNumber
.setText(LogisticCode
);}public void showDefaultInfo(){tv_showNumber
.setText("暫無");Toast
.makeText(this, "請輸入正確信息", Toast
.LENGTH_SHORT
).show();}
-此時整體邏輯基本結束。
3.3.4 RecycleView 的使用
這一節我就簡要跳過,不懂的小伙伴請百度下RecycleView的使用
public class LoadTrace {private String traceInfo
;private String time
;public LoadTrace(String traceInfo
,String time
){this.traceInfo
= traceInfo
;this.time
= time
;}public String
getTraceInfo() {return traceInfo
;}public String
getTime() {return time
;}
}
public class LoadTraceAdapter extends RecyclerView.Adapter<LoadTraceAdapter.ViewHolder> {private List
<LoadTrace>mLoadTraces
;static class ViewHolder extends RecyclerView.ViewHolder{TextView traceInfo
;TextView time
;public ViewHolder(View view
){super(view
);traceInfo
= (TextView
)view
.findViewById(R
.id
.tv_traceInfo
);time
= (TextView
)view
.findViewById(R
.id
.tv_time_item
);}}public LoadTraceAdapter(List
<LoadTrace>loadTraceList
){mLoadTraces
= loadTraceList
;}@Overridepublic ViewHolder
onCreateViewHolder(ViewGroup parent
, int viewType
){View view
= LayoutInflater
.from(parent
.getContext()).inflate(R
.layout
.loadtrace_item
,parent
,false);ViewHolder holder
= new ViewHolder(view
);return holder
;}@Overridepublic void onBindViewHolder(ViewHolder holder
,int position
){LoadTrace loadTrace
= mLoadTraces
.get(position
);holder
.traceInfo
.setText(loadTrace
.getTraceInfo());holder
.time
.setText(loadTrace
.getTime());}@Overridepublic int getItemCount(){return mLoadTraces
.size();}}
(三)將代碼push到github上
項目終于完成了! 這時候可以將我們的項目上傳到 github 上管理。
尾巴:
??? 謝謝能看到這里的小伙伴,本人也是第一次寫這么長的博客,希望你能有所收獲,也希望我們能一起努力,學好技術!
??? 要是有小伙伴不知道最終效果是什么,可以在github中download我的代碼下面是我的源代碼:
https://github.com/LQF-dev/DaydayExpress
點這里可直接跳轉
總結
以上是生活随笔為你收集整理的手把手带你完成第一个 android 小程序(快递App)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。