Android网络编程之一个Android下菜单系统模块的实现(客户端—结算功能)

在主操作界面中点击“结台”,跳转到查询支付账单界面PayActivity,先为其编写布局文件:

<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:orientation="vertical">
    
    <TableLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
        >
        <TableRow>
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="输入订单号"
                />            
            <EditText 
                android:id="@+id/orderIdEditText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />            
            <Button 
                android:id="@+id/queryOrderBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="查询"
                />            
        </TableRow>
    </TableLayout>
    
    <WebView 
        android:id="@+id/showOrderWebView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    
    <Button 
        android:id="@+id/payBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结算"
        />
    
</LinearLayout>
View Code

然后准备Activity组件与所需监听事件:

public class PayActivity extends Activity {
    
    private EditText orderIdEditText = null;
    private Button queryOrderBtn = null;
    private WebView showOrderWebView = null;
    private Button payBtn = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay);
        
        orderIdEditText = (EditText) findViewById(R.id.orderIdEditText);
        queryOrderBtn = (Button) findViewById(R.id.queryOrderBtn);
        showOrderWebView = (WebView) findViewById(R.id.showOrderWebView);
        payBtn = (Button) findViewById(R.id.payBtn);
        
        queryOrderBtn.setOnClickListener(new QueryOrderBtnLiistener());
        payBtn.setOnClickListener(new PayBtnListener());
        
    }
View Code

监听查询订单按钮方法:

private class QueryOrderBtnLiistener implements OnClickListener {

        @Override
        public void onClick(View v) {
            String orderId = PayActivity.this.orderIdEditText.getText().toString();
            if (orderId == null || orderId.equals("")) {
                Toast.makeText(PayActivity.this, "请输入订单号", Toast.LENGTH_SHORT).show();
                return;
            }
            // 使用Get方式查询订单信息
            String url = HttpUtil.BASE_URL + "servlet/QueryOrderServlet?id=" + orderId;
            String resultHtml = HttpUtil.queryStringForGet(url);
            PayActivity.this.showOrderWebView.loadData(resultHtml, "text/html", "gbk");
        }
    }
View Code

监听结算订单按钮方法

private class PayBtnListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            String orderId = PayActivity.this.orderIdEditText.getText().toString();
            if (orderId == null || orderId.equals("")) {
                Toast.makeText(PayActivity.this, "请输入订单号", Toast.LENGTH_SHORT).show();
                return;
            }
            // 使用Get方式得服务器返回结果
            String url = HttpUtil.BASE_URL + "servlet/PayServlet?id=" + orderId;
            String result = HttpUtil.queryStringForGet(url);
            Toast.makeText(PayActivity.this, result, Toast.LENGTH_SHORT).show();
        }
    }
    
}
View Code

我们还拿第20张订单试验,结算成功后出现如下画面:

然后我们来观察服务器端数据库内的数据:

如图,第20张的isPay字段已经置位,结算成功。再来观察tabletbl:

第10桌的flag字段已经清零。

至此,结算功能模块编写完毕。

原文地址:https://www.cnblogs.com/moka/p/3083706.html