Jquery Mobile初探

一个汇率换算工具。主要用到的空间就是button和edittext。jquery ajax提交请求,后台用servlet处理运算。

页面放在服务器上,本地应用只要用webview访问地址就行。

我感觉Jquery Mobile的东西不流畅,不知道Sencha touch怎么样

工程结构


index.html

 
 
  
  
  My Page 
   

  
  
  
 
  
  
  
  
  
 

 

汇率转换

 
¥
$
£

HandlerServlet.java:

package com.test.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HandlerServlet extends HttpServlet {
	
	private static final double US_TO_CN = 6.2125;
	private static final double CN_TO_US = 0.1610;
	private static final double US_TO_UK = 0.6597;
	private static final double UK_TO_US = 1.5158;
	private static final double CN_TO_UK = 0.1060;
	private static final double UK_TO_CN = 9.4172;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	    String ammount = req.getParameter("ammount");
	    String type = req.getParameter("type");
	    double us = 0;
	    double uk = 0;
	    double cn = 0;
	    double value = Double.parseDouble(ammount);
	    PrintWriter out = resp.getWriter();
	    BigDecimal big1 = new BigDecimal(value);
	    
	    if ("cn".equals(type)) {
		     BigDecimal bigUS = new BigDecimal(CN_TO_US);
		     BigDecimal bigUK = new BigDecimal(CN_TO_UK);
		     us = big1.multiply(bigUS).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		     uk = big1.multiply(bigUK).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		     
		     out.write(us+";"+uk+";"+"cn");
	    }
	    else if ("us".equals(type)) {
		     BigDecimal bigCN = new BigDecimal(US_TO_CN);
		     BigDecimal bigUK = new BigDecimal(US_TO_UK);

		     cn = big1.multiply(bigCN).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		     uk = big1.multiply(bigUK).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	    	 out.write(cn+";"+uk+";"+"us");
	    }
	    else {
		     BigDecimal bigCN = new BigDecimal(UK_TO_CN);
		     BigDecimal bigUS = new BigDecimal(UK_TO_US);
		     us = big1.multiply(bigUS).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		     cn = big1.multiply(bigCN).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	    	 out.write(us+";"+cn+";"+"uk");
	    }
	    System.out.println(us);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}

运行截图:

一个输入框改变其他2个根据相应的汇率实时算出结果

  


注:如果用WebView控件注意设置

		WebView web = (WebView)findViewById(R.id.web);
		web.getSettings().setJavaScriptEnabled(true);

不然没法加载页面js。

html缩进不要用tab键,会导致jquery mobile的组件显示不正常。



你可能感兴趣的:(Jquery Mobile初探)