2013-11-28 58 views
0

我想在用戶結束輸入後獲取edittext的值。所以默認情況下,edittext是空的。它沒有任何價值。用戶輸入一個值後,它應該得到它。Android在用戶結束輸入後獲取edittext的值

這是我已經試過:

 etQuantity.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

      Log.d("Quantity", s_quantity); 

       int i_quantity = Integer.parseInt(s_quantity); 
       int i_grossAmount = i_quantity * i_sellingPrice; 
       etGrossAmount.setText(String.valueOf(i_grossAmount)); 
       Log.d("Gross Amount", "" + String.valueOf(i_grossAmount)); 


     } 

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

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

     }  
    }); 

但是它沒有得到正確的值I輸入。有任何想法嗎?非常感謝幫助。謝謝。

+0

酪氨酸使用的鍵碼event.keyup,event.keydown解決這個 – Invader

+0

@nikhil你可以給一些代碼來做到這一點?謝謝 – Dunkey

+1

'但它沒有得到我輸入的正確值。「它得到了什麼,你認爲什麼是'正確的價值'。你能更具體地說明你想要達到的目標嗎? – FoamyGuy

回答

6

其快速的工作嘗試,希望它有助於

EditText text = (EditText)findViewById(R.id.editText1); 
text.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
      System.out.println("ONtext changed " + new String(s.toString())); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 
      System.out.println("beforeTextChanged " + new String(s.toString())); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
      System.out.println("afterTextChanged " + new String(s.toString())); 
     } 
    }); 
+0

謝謝老兄!這解決了我的問題 – Dunkey

+0

高興地幫助:-) – suja

2
final EditText et = (EditText)findViewById(R.id.editText1); 
     et.addTextChangedListener(new TextWatcher() { 

      @SuppressLint("ShowToast") 
      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // TODO Auto-generated method stub 
       Toast.makeText(MainActivity.this, "" + et.getText().toString(), 2000).show(); 

      } 

      @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 

      } 
     }); 

試試這個代碼...