ConfirmOrderActivity.kt确定点餐的界面,展示商品信息和地址信息,结算之前的操作
package com.example.takeout.ui.activity
import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.app.AppCompatActivity
import com.example.takeout.R
import com.example.takeout.model.beans.RecepitAddressBean
import com.example.takeout.utils.CommonUtil
import kotlinx.android.synthetic.main.activity_confirm_order.*
class ConfirmOrderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_confirm_order)
if (CommonUtil.checkDeviceHasNavigationBar(this)) {
activity_confirm_order.setPadding(0, 0, 0, 48.dp2px())
}
rl_location.setOnClickListener {
val intent = Intent(this, RecepitAddressActivity::class.java)
startActivityForResult(intent, 1002)
}
tvSubmit.setOnClickListener {
// val intent = Intent(this, OnlinePaymentActivity::class.java)
// startActivity(intent)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == 200) {
if (data != null) {
val address: RecepitAddressBean =
data.getSerializableExtra("address") as RecepitAddressBean
tv_name.text = address.username
//TODO:其他字段类似赋值
}
}
}
fun Int.dp2px(): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
toFloat(), resources.displayMetrics
).toInt()
}
}
activity_confirm_order.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"
android:background="#fff"
android:id="@+id/activity_confirm_order"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#468ade"
android:paddingBottom="15dp"
android:paddingTop="30dp">
<ImageButton
android:id="@+id/ib_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/abc_ic_ab_back_mtrl_am_alpha"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:text="确认订单"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_location"
android:layout_width="match_parent" android:layout_height="80dp"
android:background="#fff">
<ImageView
android:id="@+id/iv_location"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="20dp"
android:background="@mipmap/bk_icon_select_location"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_toRightOf="@id/iv_location"
android:layout_toLeftOf="@+id/iv_arrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:text="黑马程序员"/>
<TextView
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#fd282626"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:text="先生"/>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#fd282626"
android:layout_gravity="center_vertical"
android:text="13787006927,18989898989"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_label"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:maxLines="1"
android:padding="3dp"
android:textSize="10sp"
android:background="#fff"
android:gravity="center"
android:text=""
android:textColor="#fff"
android:visibility="gone"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/tv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#fd9b9999"
android:text="中关村软件园"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:orientation="horizontal">
<View
android:layout_width="50dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#fdd9d7d7"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#468ade"/>
<View
android:layout_width="15dp"
android:layout_height="15dp"
android:background="@mipmap/bk_icon_in_time"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="送达时间"
android:layout_marginLeft="10dp"
android:textColor="#000"
android:textSize="18sp"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="尽快送达 | 预计17:55"
android:textColor="#468ade"
android:textSize="12sp"
android:layout_gravity="center_vertical"
/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蜂鸟专送"
android:textColor="#fff"
android:textSize="12sp"
android:padding="3dp"
android:layout_gravity="center_vertical"
android:background="#468ade"
android:layout_marginRight="4dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="超时秒赔"
android:textColor="#468ade"
android:textSize="12sp"
android:layout_gravity="center_vertical"
/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="支付方式"
android:textSize="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="在线支付"
android:layout_toLeftOf="@id/iv_arrow"
android:textSize="12sp"/>
<ImageView
android:id="@id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="红包"
android:textSize="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#fdd9d7d7"
android:text="无红包可用"
android:enabled="false"
android:layout_toLeftOf="@id/iv_arrow"
android:textSize="12sp"/>
<ImageView
android:id="@id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="商家代金券"
android:textSize="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="没有商家代金券可用"
android:layout_toLeftOf="@id/iv_arrow"
android:textSize="12sp"/>
<ImageView
android:id="@id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:background="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_seller_name"
android:layout_toRightOf="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#000"
android:text="田老师红烧肉(海淀东北旺西路店)"
android:maxLines="1"
android:textSize="15sp"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<LinearLayout
android:id="@+id/ll_select_goods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="配送费"
android:textSize="12sp"/>
<TextView
android:id="@+id/tv_deliveryFee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="#000"
android:layout_alignParentRight="true"
android:text="¥4"
android:textSize="12sp"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#aa000000"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_CountPrice"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="待支付¥0"
android:textColor="#fff"
android:textSize="18sp" />
<TextView
android:id="@+id/tvSubmit"
android:layout_width="90dp"
android:layout_height="match_parent"
android:background="#22c222"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:text="提交订单"
android:textColor="#fff"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>
RecepitAddressActivity.kt收货地址的主界面,底下有一个新增地址的按钮,添加成功以后onRestart方法更新数据展示列表
package com.example.takeout.ui.activity
import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.takeout.R
import com.example.takeout.model.beans.RecepitAddressBean
import com.example.takeout.model.dao.AddressDao
import com.example.takeout.ui.adapter.AddressRvAdapter
import com.example.takeout.ui.view.RecycleViewDivider
import com.example.takeout.utils.CommonUtil
import kotlinx.android.synthetic.main.activity_address_list.*
class RecepitAddressActivity : AppCompatActivity() {
lateinit var addressDao: AddressDao
lateinit var adapter: AddressRvAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_address_list)
addressDao = AddressDao(this)
rv_receipt_address.layoutManager = LinearLayoutManager(this)
//rv_receipt_address增加分割线
rv_receipt_address.addItemDecoration(
RecycleViewDivider(
this,
LinearLayoutManager.HORIZONTAL
)
)
adapter = AddressRvAdapter(this)
rv_receipt_address.adapter = adapter
if (CommonUtil.checkDeviceHasNavigationBar(this)) {
activity_address_list.setPadding(0, 0, 0, 48.dp2px())
}
tv_add_address.setOnClickListener {
val intent : Intent = Intent(this, AddOrEditAddressActivity::class.java)
startActivity(intent)
}
}
/**
* onRestart中获取地址
*/
override fun onStart() {
super.onStart()
val addressList = addressDao.queryAllAddress()
if (addressList.isNotEmpty()) {
// toast("一共有" + addressList.size + "个地址")
adapter.setAddList(addressList as ArrayList<RecepitAddressBean>)
}
}
fun Int.dp2px(): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
toFloat(), resources.displayMetrics
).toInt()
}
}
AddOrEditAddressActivity.kt新增地址的操作界面,涉及到增删改查
package com.example.takeout.ui.activity
import android.content.DialogInterface
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.TypedValue
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.example.takeout.R
import com.example.takeout.model.beans.RecepitAddressBean
import com.example.takeout.model.dao.AddressDao
import com.example.takeout.utils.CommonUtil
import kotlinx.android.synthetic.main.activity_add_edit_receipt_address.*
import org.jetbrains.anko.toast
class AddOrEditAddressActivity : AppCompatActivity(), View.OnClickListener {
override fun onClick(v: View?) {
when (v?.id) {
R.id.ib_back -> finish()
R.id.ib_add_phone_other -> rl_phone_other.visibility = View.VISIBLE
R.id.ib_delete_phone -> et_phone.setText("")
R.id.ib_delete_phone_other -> et_phone_other.setText("")
R.id.ib_select_label -> selectLabel()
R.id.btn_ok -> {
val isOk = checkReceiptAddressInfo()
if (isOk) {
if (intent.hasExtra("addressBean")) {
updateAddress()
}else {
//新增地址
insertAddress()
}
}
}
R.id.btn_location_address -> {
// val intent = Intent(this, MapLocationActivity::class.java)
// startActivityForResult(intent, 1001)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == 200){
if(data!=null) {
val title = data.getStringExtra("title")
val address = data.getStringExtra("address")
et_receipt_address.setText(title)
et_detail_address.setText(address)
}
}
}
private fun updateAddress() {
var username = et_name.text.toString().trim()
var sex = "女士"
if (rb_man.isChecked) {
sex = "先生"
}
var phone = et_phone.text.toString().trim()
var phoneOther = et_phone_other.text.toString().trim()
var address = et_receipt_address.text.toString().trim()
var detailAddress = et_detail_address.text.toString().trim()
var label = tv_label.text.toString()
addressBean.username = username
addressBean.sex = sex
addressBean.phone = phone
addressBean.phoneOther = phoneOther
addressBean.address = address
addressBean.detailAddress = detailAddress
addressBean.label = label
addressDao.updateRecepitAddressBean(addressBean)
toast("更新地址成功")
finish()
}
private fun insertAddress() {
var username = et_name.text.toString().trim()
var sex = "女士"
if (rb_man.isChecked) {
sex = "先生"
}
var phone = et_phone.text.toString().trim()
var phoneOther = et_phone_other.text.toString().trim()
var address = et_receipt_address.text.toString().trim()
var detailAddress = et_detail_address.text.toString().trim()
var label = tv_label.text.toString()
addressDao.addRecepitAddressBean(RecepitAddressBean(999, username, sex, phone, phoneOther, address, detailAddress, label, "38"))
toast("新增地址成功")
finish()
}
val titles = arrayOf("无", "家", "学校", "公司")
val colors = arrayOf("#778899", "#ff3399", "#ff9933", "#33ff99")
lateinit var addressDao: AddressDao
private fun selectLabel() {
val builder = AlertDialog.Builder(this)
builder.setTitle("请选择地址标签")
builder.setItems(titles, object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, which: Int) {
tv_label.text = titles[which].toString()
tv_label.setBackgroundColor(Color.parseColor(colors[which]))
tv_label.setTextColor(Color.BLACK)
}
})
builder.show()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_edit_receipt_address)
processIntent()
addressDao = AddressDao(this)
if (CommonUtil.checkDeviceHasNavigationBar(this)) {
activity_add_address.setPadding(0, 0, 0, 48.dp2px())
}
btn_location_address.setOnClickListener(this)
ib_back.setOnClickListener(this)
ib_add_phone_other.setOnClickListener(this)
ib_delete_phone.setOnClickListener(this)
ib_delete_phone_other.setOnClickListener(this)
ib_select_label.setOnClickListener(this)
btn_ok.setOnClickListener(this)
et_phone.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
if (!TextUtils.isEmpty(s)) {
ib_delete_phone.visibility = View.VISIBLE
} else {
ib_delete_phone.visibility = View.INVISIBLE
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
et_phone_other.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
if (!TextUtils.isEmpty(s)) {
ib_delete_phone_other.visibility = View.VISIBLE
} else {
ib_delete_phone_other.visibility = View.INVISIBLE
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
}
lateinit var addressBean: RecepitAddressBean
private fun processIntent() {
if (intent.hasExtra("addressBean")) {
addressBean = intent.getSerializableExtra("addressBean") as RecepitAddressBean
tv_title.text = "修改地址"
ib_delete.visibility = View.VISIBLE
ib_delete.setOnClickListener {
addressDao.deleteRecepitAddressBean(addressBean)
toast("删除此地址成功")
finish()
}
et_name.setText(addressBean.username)
val sex = addressBean.sex
if ("先生".equals(sex)) {
rb_man.isChecked = true
} else {
rb_women.isChecked = true
}
et_phone.setText(addressBean.phone)
et_phone_other.setText(addressBean.phoneOther)
et_receipt_address.setText(addressBean.address)
et_detail_address.setText(addressBean.detailAddress)
tv_label.text = addressBean.label
}
}
fun Int.dp2px(): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
toFloat(), resources.displayMetrics).toInt()
}
fun checkReceiptAddressInfo(): Boolean {
val name = et_name.getText().toString().trim()
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "请填写联系人", Toast.LENGTH_SHORT).show()
return false
}
val phone = et_phone.getText().toString().trim()
if (TextUtils.isEmpty(phone)) {
Toast.makeText(this, "请填写手机号码", Toast.LENGTH_SHORT).show()
return false
}
if (!isMobileNO(phone)) {
Toast.makeText(this, "请填写合法的手机号", Toast.LENGTH_SHORT).show()
return false
}
val receiptAddress = et_receipt_address.getText().toString().trim()
if (TextUtils.isEmpty(receiptAddress)) {
Toast.makeText(this, "请填写收获地址", Toast.LENGTH_SHORT).show()
return false
}
val address = et_detail_address.getText().toString().trim()
if (TextUtils.isEmpty(address)) {
Toast.makeText(this, "请填写详细地址", Toast.LENGTH_SHORT).show()
return false
}
return true
}
fun isMobileNO(phone: String): Boolean {
val telRegex = "[1][358]\\d{9}"//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
return phone.matches(telRegex.toRegex())
}
}
AddressDao.kt地址数据的增删改查操作
package com.example.takeout.model.dao
import android.content.Context
import android.util.Log
import com.example.takeout.model.beans.RecepitAddressBean
import com.j256.ormlite.dao.Dao
class AddressDao(val context: Context) {
lateinit var addressDao: Dao<RecepitAddressBean, Int>
init {
val openHelper = TakeoutOpenHelper(context)
addressDao = openHelper.getDao(RecepitAddressBean::class.java)
}
fun addRecepitAddressBean(bean: RecepitAddressBean){
try{
addressDao.create(bean)
}catch (e : Exception){
Log.e("addressBean", e.localizedMessage)
}
}
fun deleteRecepitAddressBean(bean: RecepitAddressBean){
try{
addressDao.delete(bean)
}catch (e : Exception){
Log.e("addressBean", e.localizedMessage)
}
}
fun updateRecepitAddressBean(bean: RecepitAddressBean){
try{
addressDao.update(bean)
}catch (e : Exception){
Log.e("addressBean", e.localizedMessage)
}
}
fun queryAllAddress():List<RecepitAddressBean>{
try{
return addressDao.queryForAll()
}catch (e : Exception){
Log.e("addressBean", e.localizedMessage)
return ArrayList<RecepitAddressBean>()
}
}
}
RecepitAddressBean.kt地址数据库的bean文件,作为bean进行增删改查的操作
package com.example.takeout.model.beans
import com.j256.ormlite.field.DatabaseField
import com.j256.ormlite.table.DatabaseTable
import java.io.Serializable
@DatabaseTable(tableName = "t_address") class RecepitAddressBean() : Serializable {
@DatabaseField(generatedId = true)
var id: Int = 0
@DatabaseField(columnName = "username")
var username: String = ""
@DatabaseField(columnName = "sex")
var sex: String = "女"
@DatabaseField(columnName = "phone")
var phone: String = ""
@DatabaseField(columnName = "phoneOther")
var phoneOther: String = ""
@DatabaseField(columnName = "address")
var address: String = ""
@DatabaseField(columnName = "detailAddress")
var detailAddress: String = ""
@DatabaseField(columnName = "label")
var label = ""
@DatabaseField(columnName = "userId")
var userId: String = "38"
constructor(id: Int, username: String, sex: String, phone: String, phoneOther: String,
address: String, detailAddress: String, label: String, userId: String) : this() {
this.id = id
this.username = username
this.sex = sex
this.phone = phone
this.phoneOther = phoneOther
this.address = address
this.detailAddress = detailAddress
this.label = label
this.userId = userId
}
}
TakeoutOpenHelper.kt创建新的数据库用来维护地址的列表,需要更新数据库的版本号
package com.example.takeout.model.dao
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import com.example.takeout.beans.User
import com.example.takeout.model.beans.RecepitAddressBean
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
import com.j256.ormlite.support.ConnectionSource
import com.j256.ormlite.table.TableUtils
/**
* app版本 数据库版本
* 1.1版本 1 用户登录
* 1.3版本 2 地址管理
*/
class TakeoutOpenHelper(val context: Context) : OrmLiteSqliteOpenHelper(context, "takeout_kotlin.db", null, 2) {
override fun onCreate(p0: SQLiteDatabase?, connectionSource: ConnectionSource?) {
//创建user表
TableUtils.createTable(connectionSource, User::class.java)
//创建地址表
TableUtils.createTable(connectionSource, RecepitAddressBean::class.java)
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: ConnectionSource?, oldVersion: Int, newVersion: Int) {
//升级app的用户会执行此方法
TableUtils.createTable(connectionSource, RecepitAddressBean::class.java)
}
}
AddressRvAdapter.kt增加地址以后展示地址列表的界面,使用RecycleView实现
package com.example.takeout.ui.adapter
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.model.beans.RecepitAddressBean
import com.example.takeout.ui.activity.AddOrEditAddressActivity
class AddressRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var addressList = arrayListOf<RecepitAddressBean>()
fun setAddList(list: ArrayList<RecepitAddressBean>) {
this.addressList = list
notifyDataSetChanged()
}
override fun getItemCount(): Int {
return addressList.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as AddressItemHolder).bindData(addressList.get(position))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val itemView = LayoutInflater.from(context).inflate(R.layout.item_receipt_address,parent,false)
return AddressItemHolder(itemView)
}
inner class AddressItemHolder(itemView: View) :RecyclerView.ViewHolder(itemView){
val ivEdit: ImageView
val tvname: TextView
val tv_sex:TextView
val tv_phone:TextView
val tv_label:TextView
val tv_address:TextView
lateinit var address : RecepitAddressBean
init {
ivEdit = itemView.findViewById(R.id.iv_edit) as ImageView
ivEdit.setOnClickListener{
val intent = Intent(context, AddOrEditAddressActivity::class.java)
intent.putExtra("addressBean", address)
context.startActivity(intent)
}
tvname = itemView.findViewById(R.id.tv_name) as TextView
tv_sex = itemView.findViewById(R.id.tv_sex) as TextView
tv_phone = itemView.findViewById(R.id.tv_phone) as TextView
tv_label = itemView.findViewById(R.id.tv_label) as TextView
tv_address = itemView.findViewById(R.id.tv_address) as TextView
itemView.setOnClickListener {
val data = Intent()
data.putExtra("address", address)
(context as Activity).setResult(200, data)
(context as Activity).finish()
}
}
@SuppressLint("SetTextI18n")
fun bindData(address: RecepitAddressBean) {
this.address = address
tvname.text = address.username
tv_sex.text = address.sex
tv_phone.text = address.phone + "," + address.phoneOther
tv_address.text = "${address.address},${address.detailAddress}"
tv_label.text = address.label
}
}
}
RecycleViewDivider.kt给RecycleView增加分隔线
package com.example.takeout.ui.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class RecycleViewDivider
/**
* 默认分割线:高度为2px,颜色为灰色
* @param context
* *
* @param orientation 列表方向
*/
(context: Context, private val mOrientation: Int//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
) : RecyclerView.ItemDecoration() {
private var mPaint: Paint? = null
private var mDivider: Drawable? = null
private var mDividerHeight = 2//分割线高度,默认为1px
init {
if (mOrientation != LinearLayoutManager.VERTICAL && mOrientation != LinearLayoutManager.HORIZONTAL) {
throw IllegalArgumentException("请输入正确的参数!")
}
val a = context.obtainStyledAttributes(ATTRS)
mDivider = a.getDrawable(0)
a.recycle()
}
/**
* 自定义分割线
* @param context
* *
* @param orientation 列表方向
* *
* @param drawableId 分割线图片
*/
constructor(context: Context, orientation: Int, drawableId: Int) : this(context, orientation) {
mDivider = ContextCompat.getDrawable(context, drawableId)
mDividerHeight = mDivider!!.intrinsicHeight
}
/**
* 自定义分割线
* @param context
* *
* @param orientation 列表方向
* *
* @param dividerHeight 分割线高度
* *
* @param dividerColor 分割线颜色
*/
constructor(context: Context, orientation: Int, dividerHeight: Int, dividerColor: Int) : this(context, orientation) {
mDividerHeight = dividerHeight
mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
mPaint!!.color = dividerColor
mPaint!!.style = Paint.Style.FILL
}
//获取分割线尺寸
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
super.getItemOffsets(outRect, view, parent, state)
outRect.set(0, 0, 0, mDividerHeight)
}
//绘制分割线
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDraw(c, parent, state)
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent)
} else {
drawHorizontal(c, parent)
}
}
//绘制横向 item 分割线
private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {
val left = parent.paddingLeft
val right = parent.measuredWidth - parent.paddingRight
val childSize = parent.childCount
for (i in 0..childSize - 1) {
val child = parent.getChildAt(i)
val layoutParams = child.layoutParams as RecyclerView.LayoutParams
val top = child.bottom + layoutParams.bottomMargin
val bottom = top + mDividerHeight
if (mDivider != null) {
mDivider!!.setBounds(left, top, right, bottom)
mDivider!!.draw(canvas)
}
if (mPaint != null) {
canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(),
mPaint!!
)
}
}
}
//绘制纵向 item 分割线
private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
val top = parent.paddingTop
val bottom = parent.measuredHeight - parent.paddingBottom
val childSize = parent.childCount
for (i in 0..childSize - 1) {
val child = parent.getChildAt(i)
val layoutParams = child.layoutParams as RecyclerView.LayoutParams
val left = child.right + layoutParams.rightMargin
val right = left + mDividerHeight
if (mDivider != null) {
mDivider!!.setBounds(left, top, right, bottom)
mDivider!!.draw(canvas)
}
if (mPaint != null) {
canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(),
mPaint!!
)
}
}
}
companion object {
private val ATTRS = intArrayOf(android.R.attr.listDivider)
}
}
item_receipt_address.xml单个收获地址的展示item布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp"
android:background="#fff">
<LinearLayout
android:layout_toLeftOf="@+id/iv_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:text="黑马程序员"/>
<TextView
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#fd282626"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:text="先生"/>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#fd282626"
android:layout_gravity="center_vertical"
android:text="13787006927,18989898989"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_label"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:maxLines="1"
android:padding="3dp"
android:textSize="10sp"
android:background="#fff"
android:gravity="center"
android:text=""
android:textColor="#000"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/tv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#fd9b9999"
android:text="中关村软件园"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_edit"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/address_icon_edit"/>
</RelativeLayout>
activity_add_edit_receipt_address.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"
android:background="#f5f6f6f9"
android:id="@+id/activity_add_address"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#468ade"
android:paddingBottom="15dp"
android:paddingTop="30dp">
<ImageButton
android:id="@+id/ib_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="@mipmap/abc_ic_ab_back_mtrl_am_alpha"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/ib_back"
android:text="新增地址"
android:textColor="#fff"
android:textSize="20sp"/>
<ImageButton
android:id="@+id/ib_delete"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@mipmap/address_icon_delete"
android:visibility="gone"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="3dp"
android:background="#fff">
<TextView
android:id="@+id/tv_name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="联系人"
android:textColor="#000"
android:textSize="12sp"/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tv_name"
android:background="@null"
android:hint="你的姓名"
android:maxLines="1"
android:textSize="14sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#fff">
<RadioGroup
android:id="@+id/rg_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="先生"
android:textSize="14sp"/>
<RadioButton
android:id="@+id/rb_women"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="女士"
android:textSize="14sp"/>
</RadioGroup>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#fff">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="联系电话"
android:textColor="#000"
android:textSize="12sp"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:layout_toLeftOf="@+id/ib_delete_phone"
android:background="@null"
android:hint="你的手机号"
android:inputType="phone"
android:maxLines="1"
android:textSize="14sp"/>
<ImageButton
android:id="@+id/ib_delete_phone"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/ib_add_phone_other"
android:background="@mipmap/icon_close"
android:visibility="invisible"
/>
<ImageButton
android:id="@+id/ib_add_phone_other"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@mipmap/address_icon_reserve"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_phone_other"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#fff"
android:visibility="gone">
<EditText
android:id="@+id/et_phone_other"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:layout_toLeftOf="@+id/ib_delete_phone_other"
android:background="@null"
android:hint="备选电话"
android:inputType="phone"
android:maxLines="1"
android:textSize="14sp"/>
<ImageButton
android:id="@+id/ib_delete_phone_other"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@mipmap/icon_close"
android:visibility="invisible"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#fff"
android:visibility="visible">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="收货地址"
android:textColor="#000"
android:textSize="12sp"
/>
<EditText
android:id="@+id/et_receipt_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:background="@null"
android:hint="小区/写字楼/学校"
android:maxLines="1"
android:textSize="14sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#fff"
android:visibility="visible">
<Button
android:id="@+id/btn_location_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一键定位选址"
></Button>
<EditText
android:id="@+id/et_detail_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/btn_location_address"
android:background="@null"
android:hint="详细地址(如门牌号等)"
android:maxLines="1"
android:textSize="14sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#fff"
android:visibility="visible">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="标签"
android:textColor="#000"
android:textSize="12sp"
/>
<TextView
android:id="@+id/tv_label"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ib_select_label"
android:background="#fff"
android:gravity="center"
android:maxLines="1"
android:padding="3dp"
android:text=""
android:textColor="#666"
android:textSize="10sp"
/>
<ImageView
android:id="@+id/ib_select_label"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:src="@mipmap/address_icon_down"
/>
</RelativeLayout>
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#02c14b"
android:text="确定"
android:textColor="#fff"/>
</LinearLayout>
activity_address_list.xml收获地址activity的展示界面
<?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:background="#fff"
android:id="@+id/activity_address_list"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#468ade"
android:paddingBottom="15dp"
android:paddingTop="30dp">
<ImageButton
android:id="@+id/ib_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/abc_ic_ab_back_mtrl_am_alpha"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:text="收货地址"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_receipt_address"
android:layout_width="match_parent"
android:background="#c6c6c9"
android:layout_height="0dp"
android:layout_weight="1">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center">
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@mipmap/address_add_btn_icon"/>
<TextView
android:id="@+id/tv_add_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新增地址"
android:textColor="#46b3de"
android:layout_marginLeft="10dp"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
效果如下: