2015-08-22 37 views
1

我已經創建了一個默認值爲1的EditText「quantity」。我想在用戶編輯它之後獲取EditText的「quantity」的最終值,而不是給我默認值(= 1)。如何獲取EditText在用戶編輯後的值?

我怎麼能從這裏做到這一點?

EditText quantity = (EditText)findViewById(R.id.quantity); 
 
final int qty = Integer.valueOf(quantity.getText().toString()).intValue(); 
 

 
quantity.addTextChangedListener(new TextWatcher() { 
 
    @Override 
 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
 

 
    } 
 

 
    @Override 
 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
 

 
    } 
 

 
    @Override 
 
    public void afterTextChanged(Editable s) { 
 

 
    } 
 
});

+0

GET值(..)更新值' – Rustam

回答

3

得到來自`afterTextChanged s`的`afterTextChanged(Editable s)

 @Override 
     public void afterTextChanged(Editable s) { 
      try{ 
       qty = Integer.valueOf(s.toString()).intValue(); 
      }catch(Exception e){ 
       /// 
      } 
     } 
+0

我已經嘗試過這一點,但變量數量說「不能分配一個值給final變量「qty」我該怎麼辦?@Rustam – jeiidii

+1

把'qty'設爲非final類變量,因爲你不能改變final變量的值 – Rustam

+0

sorry im new in編程你是什麼意思...? – jeiidii

相關問題