如何自动生成总金额?

时间:2013-04-18 10:07:51

标签: android xml

我是Android新手,我想在用户在我的购买页面输入数量后自动生成总金额。我首先从另一个活动获得产品价格,然后我制作一个编辑文本来获取数量。之后页面将自动显示总金额。有没有人有这方面的经验。

我的代码:

public class PageBuy extends Activity {
TextView lblPID;
TextView lblName;
TextView lblAmount;
EditText quantity;
Integer total_amount;

private static final String KEY_ERROR_MSG = "error_msg";
private static final String TAG_NAME = "product_name";
private static final String TAG_PID = "product_id";
private static final String TAG_AMOUNT = "amount";
private static final String KEY_QUANTITY = "quantity";   
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagebuy);

        Intent gg = getIntent();


        String product_id = gg.getStringExtra(TAG_PID);
        String product_name = gg.getStringExtra(TAG_NAME);
        String amount = gg.getStringExtra(TAG_AMOUNT);

        lblPID = (TextView) findViewById(R.id.product_id_label);
        lblName = (TextView) findViewById(R.id.product_name_label);
        lblAmount = (TextView) findViewById(R.id.amount_label);

        lblPID.setText(product_id);
        lblName.setText(product_name);
        lblAmount.setText(amount);

        quantity = (EditText) findViewById(R.id.quantity);

        String quan = quantity.getText().toString().trim();
        System.out.println(quan +"Short");
        //UserFunctions userFunction = new UserFunctions();
        //JSONObject json = userFunction.rebuy(product_id, amount, quan);

        Button btn_confirm = (Button) findViewById(R.id.btn_confirm);
        Button btn_home = (Button) findViewById(R.id.btn_home);  

        btn_confirm.setOnClickListener(new View.OnClickListener() {

             public void onClick(View view) {
                  DatabaseHandler db = new DatabaseHandler(getApplicationContext());

                        String uid = db.getCurrentId();

                        System.out.println("SHORTALEX" + uid);

                        String product_id = lblPID.getText().toString().trim();
                        String amount = lblAmount.getText().toString();
                        String quan = quantity.getText().toString().trim();

                        UserFunctions userFunction = new UserFunctions();
                        JSONObject json = userFunction.rebuy(product_id, amount, quan,uid);

                       //DatabaseHandler2 db = new DatabaseHandler2(getApplicationContext());
                        JSONObject json_user;
                        try {   
                            System.out.println("SHORTY");
                            System.out.println(json.toString());
                            json_user = json.getJSONObject("userbuy");

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }



                        Toast.makeText(PageBuy.this, "Purchase Success", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(getApplicationContext(), PagePurchaseRecord.class);
                        startActivity(i);
                 }               
        });
        btn_home.setOnClickListener(new View.OnClickListener() {

             public void onClick(View view) {

                    Intent i = new Intent(getApplicationContext(), Main.class);
                    startActivity(i);
                }
        });     
}
}

1 个答案:

答案 0 :(得分:1)

你需要textwatcher

            quantity.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // update quanttiti here

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub

                }
            });