学习然后自己做了个支付页面的功能
页面截图
页面代码
<?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="#F1F0F0"
android:orientation="vertical">
<!--自定义ActionBar-->
<com.gx.helloword.widget.MyActionBar
android:id="@id/myActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 避免打开页面就弹出输入法-->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<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_marginTop="10dp"
android:background="@drawable/bg_layout_border_white"
android:padding="10dp">
<FrameLayout
android:id="@+id/rl_picture"
android:layout_width="90dp"
android:layout_height="90dp">
<ImageView
android:id="@+id/iv_item_result_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="商品图片"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/tv_item_result_minNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:background="@drawable/bg_tv_tag_style"
android:text="3件"
android:textColor="@color/colorWhite"
android:textSize="12sp" />
</FrameLayout>
<!--商品信息-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_item_result_productName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="商品名称"
android:textColor="@color/colorBlack"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_item_result_productInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="无商品描述"
android:textColor="@color/colorGrays"
android:textSize="14sp" />
</LinearLayout>
<!--商品价格信息-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="¥"
android:textColor="@color/colorReds"
android:textSize="24sp" />
<TextView
android:id="@+id/tv_item_result_price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="200"
android:textColor="@color/colorReds"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="门市价"
android:textColor="@color/colorGrays"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_item_result_standardPrice"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="¥210"
android:textColor="@color/colorGray"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--购买数量-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/bg_layout_border_white"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购买数量" />
<EditText
android:id="@+id/et_order_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center_horizontal"
android:hint="点击输入数量"
android:textAlignment="center"
android:textColorHint="@color/colorOrange"
android:inputType="number"
/>
</LinearLayout>
</LinearLayout>
<!--订单信息-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/bg_layout_border_white"
android:orientation="vertical"
android:padding="10dp"
>
<!--订单编号-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="订单编号" />
<TextView
android:id="@+id/et_order_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"/>
</LinearLayout>
<!--购买时间-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购买时间" />
<TextView
android:id="@+id/et_order_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"/>
</LinearLayout>
<!--商品名称-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品名称" />
<TextView
android:id="@+id/et_order_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"/>
</LinearLayout>
<!--订单金额-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="订单金额" />
<TextView
android:id="@+id/et_order_sum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"/>
</LinearLayout>
<!--购买人-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购买人" />
<TextView
android:id="@+id/et_order_buyer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--底部确认支付按钮-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_order_result_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@color/colorBlack"
android:text="加入购物车"
android:textColor="@color/colorWhite"
android:textSize="24dp" />
<Button
android:id="@+id/btn_order_result_pay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@color/colorOrange"
android:text="支 付"
android:textColor="@color/colorWhite"
android:textSize="24dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Activity代码
package com.gx.helloword.ui.order;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.gx.helloword.MyApplication;
import com.gx.helloword.R;
import com.gx.helloword.bean.Product;
import com.gx.helloword.bean.SysUser;
import com.gx.helloword.ui.MainActivity;
import com.gx.helloword.util.OkHttpTool;
import com.gx.helloword.util.ServiceUrls;
import com.gx.helloword.util.Tools;
import org.json.JSONException;
import org.json.JSONObject;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class OrderActivity extends AppCompatActivity {
ImageView iv_item_result_picture;
TextView tv_item_result_productName;
TextView tv_item_result_productInfo;
TextView tv_item_result_price;
TextView tv_item_result_standardPrice;
TextView tv_item_result_minNum;
private Product product;
private Activity mActivityContext;
private EditText et_order_count;//购买数量
private TextView et_order_sum;//订单金额
private TextView et_order_code;//订单编号
private TextView et_order_time;//购买时间
private TextView et_order_buyer;//购买人
private TextView et_order_name;//商品名称
private MyApplication myApplication;
private SysUser sysUser;
private Button btn_order_result_pay;//支付按钮
private SimpleDateFormat simpleDateFormat;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_info);
//Intent intent=getIntent().getSerializableExtra("product");
//获取从 SearchResultActivity 传来的商品信息
product = (Product) getIntent().getSerializableExtra("product");
mActivityContext=this;
myApplication = (MyApplication) getApplication();
sysUser = myApplication.getLogin();
iv_item_result_picture = findViewById(R.id.iv_item_result_picture);
tv_item_result_productName = findViewById(R.id.tv_item_result_productName);
tv_item_result_productInfo = findViewById(R.id.tv_item_result_productInfo);
tv_item_result_price = findViewById(R.id.tv_item_result_price);
tv_item_result_standardPrice = findViewById(R.id.tv_item_result_standardPrice);
tv_item_result_minNum = findViewById(R.id.tv_item_result_minNum);
et_order_count = findViewById(R.id.et_order_count);
et_order_sum = findViewById(R.id.et_order_sum);
et_order_code = findViewById(R.id.et_order_code);
et_order_time = findViewById(R.id.et_order_time);
et_order_buyer = findViewById(R.id.et_order_buyer);
et_order_name = findViewById(R.id.et_order_name);
btn_order_result_pay = findViewById(R.id.btn_order_result_pay);
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
initView();//页面初始化
setViewListener();//页面控件监听
}
private void initView() {
String picture = product.getPicture();
if (Tools.isNotNull(picture)) {
String[] pictures = picture.split(";");
if (picture.length() > 0) {
//使用glide 加载图片
String imageUrl = ServiceUrls.getReserveMethodUrl("getPicture") + "?pictureName=" + pictures[0];
Glide.with(mActivityContext)
.load(imageUrl)
.into(iv_item_result_picture);
}
}
//==设置商品数量
tv_item_result_minNum.setText(product.getNumber());
//设置商品名称
tv_item_result_productName.setText(product.getName());
//设置商品描述信息
tv_item_result_productInfo.setText(product.getDescribe());
//设置折扣价格
tv_item_result_price.setText(product.getStandardprice().toString());
//设置未折扣价格
tv_item_result_standardPrice.setText(product.getPrice().toString());
tv_item_result_standardPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//设置删除线
//设置购买数量
et_order_count.setText(product.getNumber());
//设置订单编号
Date date=new Date();
et_order_code.setText(String.format(Locale.getDefault(),"DD%s", simpleDateFormat.format(date)));
//设置购买时间
et_order_time.setText(simpleDateFormat.format(date));
//设置商品名称
et_order_name.setText(product.getName());
//设置订单金额
BigDecimal number=new BigDecimal(product.getNumber());
BigDecimal sum=number.multiply(product.getStandardprice());
et_order_sum.setText(sum.toString());
//设置购买人
//et_order_buyer.setText(sysUser.getPhone());
et_order_buyer.setText("13068524606");
}
private void setViewListener() {
//监听购买数量文本值改变
et_order_count.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text=s.toString().equals("")?"0":s.toString();
if(text.length()<11){
BigDecimal a=new BigDecimal(Integer.parseInt(text));
BigDecimal b=a.multiply(product.getStandardprice());
et_order_sum.setText(b.toString());
}else {
Toast.makeText(mActivityContext,"最多输入10位数!留点给别人吧",Toast.LENGTH_SHORT).show();
}
}
});
btn_order_result_pay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取图片名称
String getPicture = product.getPicture();
String picture="";
if (Tools.isNotNull(getPicture)) {
String[] pictures = getPicture.split(";");
if (getPicture.length() > 0) {
picture = pictures[0];
}
}
//获取商品名称
String name=et_order_name.getText()==null?"":et_order_name.getText().toString();
//获取购买数量
int buy=et_order_count.getText()==null?0:Integer.parseInt(et_order_count.getText().toString());
//获取订单编号
String order_code=et_order_code.getText()==null?"":et_order_code.getText().toString();
//获取购买时间
String buy_time= et_order_time.getText().toString();
//订单金额
BigDecimal money=product.getStandardprice().multiply(BigDecimal.valueOf(buy));
//BigDecimal money=BigDecimal.valueOf(Integer.parseInt(et_order_sum.getText().toString()));
//获取购买人
//int userid=sysUser.getUserid();
int userid=1;
String url=ServiceUrls.getReserveMethodUrl("orderDetailsInsert");
Map<String,Object> map=new HashMap<>();
map.put("picture",picture);
map.put("buy",buy);
map.put("orderCode",order_code);
map.put("buyTime",buy_time);
map.put("money",money);
map.put("userid",userid);
map.put("productName",name);
OkHttpTool.httpPost(url,map, new OkHttpTool.ResponseCallback() {
@Override
public void onResponse(boolean isSuccess, int responseCode, String response, Exception exception) {
mActivityContext.runOnUiThread(new Runnable() {
@Override
public void run() {
if (isSuccess && responseCode==200){
String strText = "";
try {
JSONObject jsonObject = new JSONObject(response);
int code = jsonObject.getInt("code");
strText = jsonObject.getString("text");
if (code == 200) {
Toast.makeText(mActivityContext, strText, Toast.LENGTH_LONG).show();
}else {
Toast.makeText(mActivityContext, "支付失败", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(mActivityContext, "支付失败", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
});
}
});
}
});
}
}