当用户在android中输入值时,如何将两个editText的值相乘并在textView中获得值

时间:2014-04-10 07:42:39

标签: android calculator textchanged

我在我的活动中有两个EditText和一个TextView,我想多个都是edittext值的东西是当用户输入任何edittext的值时,相应的textView值应该改变。我的代码如下:

EDITTEXTS:数量,价格 的TextView:tv_subtotal

main.java

package com.epe.yehki.ui;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epe.yehki.util.Const;
import com.epe.yehki.util.Pref;
import com.epe.yehki.util.Utils;
import com.example.yehki.R;

public class PlaceOrderActivity extends ListActivity {
    Button confirm, shipping;
    RelativeLayout rl_adress;
    TextView product_name;
    TextView tv_adress;
    TextView tv_select_adres;
    EditText qty;
    EditText price;
    TextView tv_subtotal;
    TextView tv_total;
    ArrayAdapter<String> adapter;
    String method;
    ImageView back;
    Double total, subtotal, min_qty, min_agree_prc, max_agree_prc, min_agree_qty, Max_agree_qty, min_qty_unit_name, max_price, min_price;
    Intent i = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_place_order);
        final ListView lv = getListView();
        System.out.println("::::::::::::my  custome Id inside ::::::::::place order" + Pref.getValue(PlaceOrderActivity.this, Const.PREF_CUSTOMER_ID, ""));
        // FINDINLG IDS...!!
        qty = (EditText) findViewById(R.id.et_qty);
        price = (EditText) findViewById(R.id.et_price);
        rl_adress = (RelativeLayout) findViewById(R.id.btn_adress);
        product_name = (TextView) findViewById(R.id.tv_product_name);
        tv_adress = (TextView) findViewById(R.id.tv_adress);
        tv_select_adres = (TextView) findViewById(R.id.tv_select_adress);
        tv_total = (TextView) findViewById(R.id.tv_total);
        tv_subtotal = (TextView) findViewById(R.id.tv_subtotal);
        back = (ImageView) findViewById(R.id.iv_back);
        qty.setCursorVisible(false);
        price.setCursorVisible(false);

        lv.setVisibility(View.GONE);
        String[] shipMethods = new String[] { "Sea Freight", "Air Cargo", "Express" };
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, shipMethods);

        shipping = (Button) findViewById(R.id.btn_shiping_method);
        shipping.setText("Select shipping Method");
        if (Pref.getValue(PlaceOrderActivity.this, Const.FLAG_ADDRESS, "").equals("")) {

            max_agree_prc = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MAX_PRICE));
            min_agree_prc = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MIN_PRICE));
            Max_agree_qty = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MAX_QTY));
            min_agree_qty = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MIN_QTY));
            min_qty = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MIN_ORDER_QTY));
            min_qty_unit_name = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MIN_ORDER_QTY_UNIT_NAME));
            max_price = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MAX_PRICE));
            min_price = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MIN_PRICE));
            product_name.setText(getIntent().getStringExtra(Const.TAG_PRODUCT_NAME));
        }
        qty.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                qty.setCursorVisible(true);

            }
        });

        price.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                price.setCursorVisible(true);
            }
        });

        // SELECT ADDRESS CLICK EVENT..!
        rl_adress.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                i = new Intent(PlaceOrderActivity.this, AddressListActivity.class);
                startActivity(i);
            }
        });
        shipping.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                setListAdapter(adapter);
                lv.setVisibility(View.VISIBLE);

            }
        });
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                method = (String) getListAdapter().getItem(position);
                shipping.setText(method);
                i.putExtra("shipping", method);
                lv.setVisibility(View.GONE);
            }

        });
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Checking for quantity and price...!!!
                if (Integer.parseInt(qty.getText().toString()) >= min_qty) {
                    // check for quantity range
                    if (Integer.parseInt(price.getText().toString()) >= min_price && Integer.parseInt(price.getText().toString()) <= max_price) {
                        // check for price range

                    } else {
                        Utils.showCustomeAlertValidation(PlaceOrderActivity.this, "Enter Price in Range", "ALERT", "OK");
                    }

                } else {
                    Utils.showCustomeAlertValidation(PlaceOrderActivity.this, "Enter Quantity in Range", "ALERT", "OK");
                }

            }

        });

    }

    private void calculate() {
        // TODO Auto-generated method stub

        double QuantyInt = 1;
        double PriceInt = 0;

        if (qty != null)

            QuantyInt = Double.parseDouble(qty.getText().toString());

        if (price != null)
            PriceInt = Double.parseDouble(price.getText().toString());

        subtotal = (QuantyInt * PriceInt);
        String textResult = "Your BMI is " + subtotal;
        tv_subtotal.setText(textResult);
        tv_total.setText(textResult);

    }
}

0 个答案:

没有答案