我希望在用户结束输入后获得edittext的值。因此默认情况下,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
}
});
但它没有得到我输入的正确值。有任何想法吗?非常感谢帮助。感谢。
答案 0 :(得分: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()));
}
});
答案 1 :(得分: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
}
});
试试这段代码......