2013-04-18 65 views
0

我是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); 
       } 
     });  
} 
} 
+0

不使用'System.out.println',使用'android.util.Log' – tbruyelle

回答

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 

       } 
      });