廢話不多說,效果圖如下:
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請求服務器代碼