一.快遞查詢開發
此效果展示:
1.新建CourierActivity,編寫界面交互代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:padding="10dp" tools:context=".ui.CourierActivity"> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_courier_company" android:text="zto"/> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_courier_number" android:text="416688878066"/> <Button android:id="@+id/btn_get_courier" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_bg" android:text="查詢" android:textColor="@android:color/white"/> <ListView android:id="@+id/mListView" android:layout_width="match_parent" android:layout_height="0dp" android:scrollbars="none" android:layout_weight="1"/> </LinearLayout>
2.編寫Kotlin界面交互代碼:
package com.zrc.smartbutler.ui import android.os.Bundle import android.text.TextUtils import android.widget.Toast import com.kymjs.rxvolley.RxVolley import com.kymjs.rxvolley.client.HttpCallback import com.zrc.smartbutler.R import com.zrc.smartbutler.adapter.CourierAdapter import com.zrc.smartbutler.entity.CourierData import com.zrc.smartbutler.utils.L import kotlinx.android.synthetic.main.activity_courier.* import org.json.JSONException import org.json.JSONObject import java.util.* import kotlin.collections.ArrayList // 快遞查詢 class CourierActivity : BaseActivty() { var mList = ArrayList<CourierData>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_courier) back() initView(); } private fun initView() { //查詢 btn_get_courier.setOnClickListener { /** * 1.獲取輸入框的內容 * 2.判斷是否為空 * 3.拿到數據去請求數據(JSON) * 4.解析JSON * 5.ListView適配器 * 6.實體類(item) * 7.設置數據/顯示效果 */ //1.獲取輸入框的內容 val name = et_name.text.trim() val number = et_number.text.trim() //拼接URL val url = "http://10.0.2.2/abc.json" //2.判斷是否為空 if(!TextUtils.isEmpty(name) and !TextUtils.isEmpty(number)){ //3.拿到數據去請求數據(Json) RxVolley.get(url, object : HttpCallback() { override fun onSuccess(t: String) { //Toast.makeText(, t, Toast.LENGTH_SHORT).show(); L().i("Courier:$t") //4.解析Json parsingJson(t) } }) }else{ Toast.makeText(this,"輸入框不能為空",Toast.LENGTH_SHORT).show() } } } //解析數據 private fun parsingJson(t: String) { try { val jsonObject = JSONObject(t) val jsonResult = jsonObject.getJSONObject("result") val jsonArray = jsonResult.getJSONArray("list") for (i in 0 until jsonArray.length()) { val json = jsonArray[i] as JSONObject val data = CourierData() data.setRemark(json.getString("remark")) data.setZone(json.getString("zone")) data.setDatetime(json.getString("datetime")) mList.add(data) } //倒序 Collections.reverse(mList) L().i("Courier:$mList") val adapter = CourierAdapter(this, R.layout.layout_courier_item,mList) mListView.adapter = adapter } catch (e: JSONException) { e.printStackTrace() } } }
由于快遞查詢免費次數耗盡,所以制作本地接口,作為演示。
具體操作步驟在上面代碼注釋中,已經很明確,在這就不過多贅述,下面附上適配器和布局代碼。
拿到數據去請求數據(JSON)
RxVolley.get(url, object : HttpCallback() { override fun onSuccess(t: String) { //Toast.makeText(, t, Toast.LENGTH_SHORT).show(); L().i("Courier:$t") //4.解析Json parsingJson(t) } })
ListView適配器:
package com.zrc.smartbutler.adapter /** *項目名: SmartButler *包名: com.zrc.smartbutler.adapter *文件名: CourierAdapter *描述: 快遞查詢適配器 */ import android.app.Activity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter import android.widget.TextView import com.zrc.smartbutler.R import com.zrc.smartbutler.entity.CourierData class CourierAdapter(activity:Activity, val resourceId:Int,mList: List<CourierData>) : ArrayAdapter<CourierData>(activity,resourceId,mList){ override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val view:View val viewHolder:ViewHolder if(convertView==null){ view = LayoutInflater.from(context).inflate(resourceId,parent,false) val tv_remark:TextView = view.findViewById(R.id.tv_remark) val tv_zone:TextView = view.findViewById(R.id.tv_zone) val tv_datetime:TextView = view.findViewById(R.id.tv_datetime) viewHolder = ViewHolder(tv_remark,tv_zone,tv_datetime) view.tag = viewHolder }else{ view = convertView viewHolder = view.tag as ViewHolder } val fruit = getItem(position) if(fruit!=null){ viewHolder.tv_remark.text = fruit.getRemark() viewHolder.tv_zone.text = fruit.getZone() viewHolder.tv_datetime.text = fruit.getDatetime() } return view } inner class ViewHolder(val tv_remark:TextView,val tv_zone:TextView,val tv_datetime:TextView) }
layout_courier_item布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="60dp" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <View android:layout_width="2dp" android:layout_height="30dp" android:background="@color/colorPrimary"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/timeline_green"/> <View android:layout_width="2dp" android:layout_height="100dp" android:background="@color/colorPrimary"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_weight="1" android:background="@drawable/timeline_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/tv_remark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:text="你的快件已經到達北京" android:textColor="@color/colorPrimary" android:textSize="18sp"/> <TextView android:id="@+id/tv_zone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:paddingLeft="10dp" android:text="北京" android:textColor="@color/colorAccent" android:textSize="15sp"/> <TextView android:id="@+id/tv_datetime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:text="2016-11-12"/> </LinearLayout> </LinearLayout>
至此,快遞查詢開發完成?。?!
二.號碼地查詢開發
效果展示:
1.新建PhoneActivity,編寫xml代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:padding="10dp" tools:context=".ui.PhoneActivity"> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_input_phone"/> <LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv_company" android:layout_width="200dp" android:layout_height="100dp" android:padding="30dp"/> <TextView android:gravity="center" android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorPrimaryDark" android:textSize="20sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical"> <!--第一行--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn_1" android:textSize="20sp" android:text="1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_2" android:textSize="20sp" android:text="2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_3" android:textSize="20sp" android:text="3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_del" android:textSize="20sp" android:text="DEL" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <!--第二行--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn_4" android:textSize="20sp" android:text="4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_5" android:textSize="20sp" android:text="5" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_6" android:textSize="20sp" android:text="6" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_0" android:textSize="20sp" android:text="0" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <!--第三行--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btn_7" android:textSize="20sp" android:text="7" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_8" android:textSize="20sp" android:text="8" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_9" android:textSize="20sp" android:text="9" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/btn_query" android:textSize="20sp" android:text="@string/text_query" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> </LinearLayout> </LinearLayout>
2.編寫kotlin交互代碼:
package com.zrc.smartbutler.ui import android.os.Bundle import android.text.TextUtils import android.view.View import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.kymjs.rxvolley.RxVolley import com.kymjs.rxvolley.client.HttpCallback import com.zrc.smartbutler.R import com.zrc.smartbutler.utils.L import com.zrc.smartbutler.utils.StaticClass import kotlinx.android.synthetic.main.activity_phone.* import org.json.JSONException import org.json.JSONObject class PhoneActivity : BaseActivty() ,View.OnClickListener{ //標記位 private var flag = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_phone) back() initView() } private fun initView() { btn_0.setOnClickListener(this) btn_1.setOnClickListener(this) btn_2.setOnClickListener(this) btn_3.setOnClickListener(this) btn_4.setOnClickListener(this) btn_5.setOnClickListener(this) btn_6.setOnClickListener(this) btn_7.setOnClickListener(this) btn_8.setOnClickListener(this) btn_9.setOnClickListener(this) btn_query.setOnClickListener(this) btn_del.setOnClickListener(this) //長按事件 btn_del.setOnLongClickListener { et_number.setText("") false } } //點擊事件 /** * 邏輯 * 1.獲取輸入框的內容 * 2.判斷是否為空 * 3.網絡請求 * 4.解析Json * 5.結果顯示 * * ------ * 鍵盤邏輯 */ override fun onClick(v: View?) { //獲取到輸入框的內容 var str = et_number.text.toString() when (v!!.id) { R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8, R.id.btn_9 -> { if (flag) { flag = false str = "" et_number.setText("") } //每次結尾添加1 et_number.setText(str + (v as Button).text) //移動光標 et_number.setSelection(str.length + 1) } R.id.btn_del -> if (!TextUtils.isEmpty(str) && str.length > 0) { //每次結尾減去1 et_number.setText(str.substring(0, str.length - 1)) //移動光標 et_number.setSelection(str.length - 1) } R.id.btn_query -> if (!TextUtils.isEmpty(str)) { getPhone(str) } } } //獲取歸屬地 private fun getPhone(str: String) { val url = "http://apis.juhe.cn/mobile/get?phone=" + str + "&key=" + StaticClass().PHONE_KEY RxVolley.get(url, object : HttpCallback() { override fun onSuccess(t: String) { //Toast.makeText(this@PhoneActivity, "結果:" + t, Toast.LENGTH_SHORT).show(); L().i("phone:$t") parsingJson(t) } }) } /** * "province":"浙江", * "city":"杭州", * "areacode":"0571", * "zip":"310000", * "company":"中國移動", * "card":"移動動感地帶卡" */ //解析Json private fun parsingJson(t: String) { try { val jsonObject = JSONObject(t) val jsonResult = jsonObject.getJSONObject("result") val province = jsonResult.getString("province") val city = jsonResult.getString("city") val areacode = jsonResult.getString("areacode") val zip = jsonResult.getString("zip") val company = jsonResult.getString("company") val card = jsonResult.getString("card") tv_result.text = """ 歸屬地:$province$city 區號:$areacode 郵編:$zip 運營商:$company 類型:$card """.trimIndent() when (company) { "移動" -> iv_company.setBackgroundResource(R.drawable.china_mobile) "聯通" -> iv_company.setBackgroundResource(R.drawable.china_unicom) "電信" -> iv_company.setBackgroundResource(R.drawable.china_telecom) } flag = true } catch (e: JSONException) { e.printStackTrace() } } }
具體代碼內容不再贅述,注釋中已經寫的很詳細了?。?!
至此,號碼地查詢完成?。?!
快遞及號碼地查詢完成,下篇文章將針對語音機器人聊天進行開發,歡迎關注后續更新?。?!
原文地址:https://blog.csdn.net/weixin_43912367/article/details/106063338