废话不多说,效果图如下:
xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/sliver"
android:orientation="vertical"
android:padding="10dp">
<RelativeLayout
android:id="@+id/order_ticket"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorButton">
<TextView
android:id="@+id/travel_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:text="南京"
android:textColor="@color/colorFont"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="@+id/arrive_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="40dp"
android:layout_marginRight="40dp"
android:text="淮安"
android:textColor="@color/colorFont"
android:textSize="30sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="50dp"
android:layout_toLeftOf="@+id/arrive_city"
android:layout_toRightOf="@+id/travel_city"
android:background="@drawable/fo" />
<TextView
android:id="@+id/order_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="40dp"
android:text="time"
android:textColor="@color/colorFont" />
<TextView
android:id="@+id/order_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="75dp"
android:text="date"
android:textColor="@color/colorFont" />
<TextView
android:id="@+id/travel_station"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/travel_city"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="南京总站"
android:textColor="@color/colorFont"
android:textSize="20sp" />
<TextView
android:id="@+id/arrive_station"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/arrive_city"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:text="淮安总站"
android:textColor="@color/colorFont"
android:textSize="20sp" />
<TextView
android:id="@+id/order_bus_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/travel_station"
android:layout_marginLeft="30dp"
android:padding="10dp"
android:text="车次"
android:textColor="@color/colorFont" />
<TextView
android:id="@+id/order_bus_seat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/arrive_station"
android:layout_alignParentRight="true"
android:layout_marginRight="45dp"
android:padding="10dp"
android:text="座位号"
android:textColor="@color/colorFont" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/colorFont"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="乘客" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginVertical="10dp"
android:background="@color/colorGray">
</View>
<TextView
android:id="@+id/order_passenger_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="张三"
android:textStyle="bold" />
<TextView
android:id="@+id/order_passenger_idcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="3208821995" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginVertical="10dp"
android:background="@color/colorGray">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorYellow"
android:text="订单状态"/>
<TextView
android:id="@+id/gaiqian"
android:layout_marginLeft="100dp"
android:padding="2dp"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:background="@color/colorBackground"
android:textAlignment="center"
android:text="改签"/>
<TextView
android:id="@+id/tuipiao"
android:padding="2dp"
android:layout_marginLeft="20dp"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:background="@color/colorBackground"
android:textAlignment="center"
android:text="退票"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/order_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/colorFont"
android:padding="10dp">
<TextView
android:id="@+id/order_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="订单号"/>
<TextView
android:id="@+id/order_detail_price"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥ 110"
/>
</RelativeLayout>
<TextView
android:id="@+id/order_create"
android:background="@color/colorFont"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="交易时间"/>
<Button
android:id="@+id/pay_button"
android:text="立即付款"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:visibility="invisible"
android:background="@color/colorYellow"
android:textColor="@color/colorFont"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
xml文件中的颜色代码属于自定义代码,请根据实际需求自定义
java代码
package com.xujun.busticketsystem;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.alipay.sdk.app.PayTask;
import com.xujun.busticketsystem.alipay.AlipayConfig;
import com.xujun.busticketsystem.alipay.AuthResult;
import com.xujun.busticketsystem.alipay.OrderInfoUtil2_0;
import com.xujun.busticketsystem.alipay.PayResult;
import com.xujun.busticketsystem.alipay.SignUtils;
import com.xujun.busticketsystem.entity.Order;
import com.xujun.busticketsystem.utils.GetPostUtil;
import com.xujun.busticketsystem.utils.TimeFormat;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import static com.xujun.busticketsystem.alipay.AlipayConfig.APPID;
public class OrderDetailActivity extends AppCompatActivity {
private static final int SDK_PAY_FLAG = 1;
private static final int SDK_AUTH_FLAG = 2;
private TextView mTravelCity;//出发城市
private TextView mArriveCity;//到达城市
private TextView mBusTime;//车出发时间
private TextView mTravelDate;//出发日期
private TextView mTravelStation;//出发车站
private TextView mArriveStation;//到达车站
private TextView mBusNumber;//车次
private TextView mSeat;//座位号
private TextView mPassengerName;//乘客姓名
private TextView mPassengerIdCard;//乘客身份证号
private TextView mStatus;//订单状态
private TextView mGai;//改签
private TextView mTui;//退票
private TextView mOrderNumber;//订单号
private TextView mOrderPrice;//订单价格
private TextView mOrderCreateTime;//交易时间
private Button mPay;//立即支付按钮
private String orderNumber;//订单号
// @SuppressLint("HandlerLeak")
// private Handler mHandler = new Handler() {
// @SuppressWarnings("unused")
// public void handleMessage(Message msg) {
// switch (msg.what) {
// case SDK_PAY_FLAG: {
// @SuppressWarnings("unchecked")
// PayResult payResult = new PayResult((Map<String, String>) msg.obj);
// /**
// 对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
// */
// String resultInfo = payResult.getResult();// 同步返回需要验证的信息
// String resultStatus = payResult.getResultStatus();
// // 判断resultStatus 为9000则代表支付成功
// if (TextUtils.equals(resultStatus, "9000")) {
// // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
// Toast.makeText(OrderDetailActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
// } else {
// // 该笔订单真实的支付结果,需要依赖服务端的异步通知。
// Toast.makeText(OrderDetailActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
// }
// break;
// }
// case SDK_AUTH_FLAG: {
// @SuppressWarnings("unchecked")
// AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
// String resultStatus = authResult.getResultStatus();
//
// // 判断resultStatus 为“9000”且result_code
// // 为“200”则代表授权成功,具体状态码代表含义可参考授权接口文档
// if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
// // 获取alipay_open_id,调支付时作为参数extern_token 的value
// // 传入,则支付账户为该授权账户
// Toast.makeText(OrderDetailActivity.this,
// "授权成功\n" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT)
// .show();
// } else {
// // 其他状态值则为授权失败
// Toast.makeText(OrderDetailActivity.this,
// "授权失败" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT).show();
//
// }
// break;
// }
// default:
// break;
// }
// };
// };
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_details);
//实例化
mTravelCity = findViewById(R.id.travel_city);
mArriveCity = findViewById(R.id.arrive_city);
mBusTime = findViewById(R.id.order_time);
mTravelDate = findViewById(R.id.order_date);
mTravelStation = findViewById(R.id.travel_station);
mArriveStation = findViewById(R.id.arrive_station);
mBusNumber = findViewById(R.id.order_bus_number);
mSeat = findViewById(R.id.order_bus_seat);
mPassengerName = findViewById(R.id.order_passenger_name);
mPassengerIdCard = findViewById(R.id.order_passenger_idcard);
mStatus = findViewById(R.id.status);
mGai = findViewById(R.id.gaiqian);
mTui = findViewById(R.id.tuipiao);
mOrderNumber = findViewById(R.id.order_number);
mOrderPrice = findViewById(R.id.order_detail_price);
mOrderCreateTime = findViewById(R.id.order_create);
mPay = findViewById(R.id.pay_button);
Intent intent = getIntent();
String orderNumber = intent.getStringExtra("orderNumber");
getResponse(orderNumber,"查询");
//设置点击事件
mGai.setOnClickListener(listener);
mTui.setOnClickListener(listener);
//当订单处于未付款状态时,立即付款点击事件
mPay.setOnClickListener(listener);
}
/**
* 支付宝支付业务
*
* @param v
*/
// public void payV2(View v) {
// if (TextUtils.isEmpty(APPID) || TextUtils.isEmpty(AlipayConfig.RSA_PRIVATE)) {
// new AlertDialog.Builder(this).setTitle("警告").setMessage("需要配置APPID | RSA_PRIVATE")
// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialoginterface, int i) {
// //
// finish();
// }
// }).show();
// return;
// }
// Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);
// String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
//
// String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
// String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);
// final String orderInfo = orderParam + "&" + sign;
// Runnable payRunnable = new Runnable() {
//
// @Override
// public void run() {
// //从服务端获取orderInfo
// String orderInfo;
// PayTask alipay = new PayTask(OrderDetailActivity.this);
// Map<String, String> result = alipay.payV2(orderInfo, true);
// Log.i("msp", result.toString());
//
// Message msg = new Message();
// msg.what = SDK_PAY_FLAG;
// msg.obj = result;
// mHandler.sendMessage(msg);
// }
// };
//
// Thread payThread = new Thread(payRunnable);
// payThread.start();
// }
//改签、退票
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.pay_button:
//截取订单信息中的订单号
orderNumber = mOrderNumber.getText().toString().trim();
orderNumber = orderNumber.substring(5);
Log.d("orderNumber", "onClick: " + orderNumber);
String priceStr = mOrderPrice.getText().toString();
double price = Double.valueOf(priceStr.substring(1).trim());
Log.d("订单金额", "onClick: " + price);
Intent intent = new Intent(OrderDetailActivity.this,PayActivity.class);
intent.putExtra("orderNumber",orderNumber);
intent.putExtra("price",price);
startActivityForResult(intent,66);
break;
case R.id.gaiqian:
/**
* 改签必须是未出行状态的订单
* 根据出发城市、到达城市重新选择车次
* 添加改签的订单后删除被改签的订单
*/
//1.获取订单状态并判断,非未出行状态的提示不能改签的消息
String status = mStatus.getText().toString().trim();
if (!"未出行".equals(status)){
Toast.makeText(OrderDetailActivity.this,"该订单无法改签",Toast.LENGTH_SHORT).show();
return;
}
//2.根据订单号将被改签订单状态更改为改签中(状态码3)
orderNumber = mOrderNumber.getText().toString().trim();
orderNumber = orderNumber.substring(5);
Log.d("orderNumber", "onClick: " + orderNumber);
modify(orderNumber,"修改",3);
break;
case R.id.tuipiao:
/**
* 退票后订单状态转为退票中,并交由管理员审核并进行退票
* 管理员退票成功后删除订单
*/
//根据订单编号修改订单状态为退票中(状态码 6)
Log.d("orderNumber", "onClick: " + mOrderNumber.getText().toString().trim().substring(5));
new Thread(){
@Override
public void run() {
super.run();
String params = null;
try {
params = "orderNumber=" + URLEncoder.encode(mOrderNumber.getText().toString().trim().substring(5), "UTF-8") + "&option=" + URLEncoder.encode("修改", "UTF-8")
+ "&statusId=" + URLEncoder.encode("6", "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String response = GetPostUtil.executeHttpPost("order", params);
Log.e("response", response);
if (response == null || "".equals(response)) {
Toast.makeText(OrderDetailActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
return;
}
if ("ok".equals(response)){
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(OrderDetailActivity.this,OrderActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("flag",1);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
}
}
}.start();
break;
}
}
};
/**
* get the sdk version. 获取SDK版本号
*
*/
// public void getSDKVersion() {
// PayTask payTask = new PayTask(this);
// String version = payTask.getVersion();
// Toast.makeText(this, version, Toast.LENGTH_SHORT).show();
// }
private void getData(){
String travelCity = mTravelCity.getText().toString().trim();
String arriveCity = mArriveCity.getText().toString().trim();
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int week = calendar.get(Calendar.DAY_OF_WEEK);
String s = DateActivity.getWeek(week);
String date = month + "月" + day + "日 " + s;
//yyyy-MM-dd
String travelDate = year + "-" + month + "-" + day;
Intent intent = new Intent(OrderDetailActivity.this,TicketListActivity.class);
intent.putExtra("from_to", travelCity + "-" + arriveCity);
intent.putExtra("date", date);
intent.putExtra("travelDate", travelDate);
startActivity(intent);
}
private void modify(final String orderNumber, final String option, final Integer statusId) {
new Thread() {
public void run() {
String params = null;
try {
params = "orderNumber=" + URLEncoder.encode(orderNumber, "UTF-8") + "&option=" + URLEncoder.encode(option, "UTF-8")
+ "&statusId=" + URLEncoder.encode(statusId+"", "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String response = GetPostUtil.executeHttpPost("order", params);
Log.e("response", response);
if (response == null || "".equals(response)) {
Toast.makeText(OrderDetailActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
return;
}
if ("ok".equals(response)){
runOnUiThread(new Runnable() {
@Override
public void run() {
//3.获取出发城市和到达城市并执行跳转
getData();
}
});
}
}
}.start();
}
private void getResponse(final String orderNumber, final String option) {
new Thread() {
public void run() {
String params = "";
SharedPreferences sharedPreferences = getSharedPreferences("busApp", MODE_PRIVATE);
//如果不为空
if (sharedPreferences != null) {
String userName = sharedPreferences.getString("username", "");
try {
params = "username=" + URLEncoder.encode(userName, "UTF-8") + "&orderNumber=" + URLEncoder.encode(orderNumber, "UTF-8") + "&option=" + URLEncoder.encode(option, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String response = GetPostUtil.executeHttpPost("order", params);
Log.e("response", response);
if (response == null || "".equals(response)) {
Toast.makeText(OrderDetailActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
return;
}
//解析response
final Order order = new Order();
if (!"none".equals(response)) {
//分离每个订单信息
String[] strings = response.split(";");
for (String s : strings) {
//去掉[]
s = s.substring(1, s.length() - 1);
//分离出每一个单位
String[] split = s.split(",");
//封装
try {
// order.setId(Long.valueOf(split[0]));
order.setOrderNumber(split[1]);
order.setAccountId(Long.valueOf(split[2]));
order.setPassengerId(Long.valueOf(split[3]));
order.setPassengerName(split[4]);
order.setPassengerIdCard(split[5]);
order.setTrainNumber(split[6]);
order.setFromCity(split[7]);
order.setFromStation(split[8]);
order.setToCity(split[9]);
order.setToStation(split[10]);
order.setBusType(split[11]);
order.setSeat(Integer.valueOf(split[12]));
order.setPrice(Double.valueOf(split[13]));
order.setOrderStatus(Integer.valueOf(split[14]));
order.setOrderStatusName(split[15]);
order.setTravelDate(split[16]);
order.setBusTime(split[17]);
order.setCreateTime(split[18]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
updateUI(order);
}
});
}
}.start();
}
private void updateUI(Order order) {
mTravelCity.setText(order.getFromCity());
mArriveCity.setText(order.getToCity());
mBusTime.setText(order.getBusTime());
mTravelDate.setText(order.getTravelDate());
mTravelStation.setText(order.getFromStation());
mArriveStation.setText(order.getToStation());
mBusNumber.setText("车次: " + order.getTrainNumber());
mSeat.setText("座位号: " +order.getSeat());
mPassengerName.setText(order.getPassengerName());
mPassengerIdCard.setText(order.getPassengerIdCard());
mStatus.setText(order.getOrderStatusName());
mOrderNumber.setText("订单编号: " + order.getOrderNumber());
mOrderPrice.setText("¥ " + order.getPrice());
mOrderCreateTime.setText("交易时间: " + order.getCreateTime().substring(0,order.getCreateTime().length()-2));
mPay.setVisibility(View.INVISIBLE);
if ("待付款".equals(order.getOrderStatusName())){
mPay.setVisibility(View.VISIBLE);
}
}
}
java中请求代码请参阅get与post请求服务器代码