Android EditText : getting entered text
I want to get the after entered text.I have done using TextWatcher
.
There are some issues:
For example I want to enter 32.5. In that method I want to add to SET<Product>
.
Here each & every number its saving object.That means after enter 3 its add Product object into SET, then add 2 then a开发者_Go百科lso adding...
I want to avoid. Once I finish enter EditText
,Then want to take it:
final EditText txtQty = new EditText(this);
txtQty.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.v("TAG", "afterTextChanged" + s.toString());
String enterdPrice = txtPrice.getText().toString();
double remainQty =0.00;
Product enterdProduct = new Product();
try{
String enteredQty = s.toString();
enterdProduct.setProductCode(txtCode.getText().toString());
enterdProduct.setPrice(Double.parseDouble(enterdPrice));
//enterdProduct.setQty(Double.parseDouble(enteredQty));
// TO-DO
if (productSet.contains(enterdProduct)) {
productSet.remove(enterdProduct);
}
productSet.add(enterdProduct);
System.out.println("SIZE --" + productSet.size());
}
catch (Exception e) {
e.printStackTrace();
}
});
Please give me idea, How we can get the EditText once enter I enetred text?
You can get entered text on pressing "Enter" button on keyboard.
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
return true;
}
return false;
}
});
Code from Android tutorial
Can't you use a separate button to add an object using the value in EditText?
Extend the EditText
class and override onEndBatchEdit
to implement the saving functionality after it has been edited in a 'batch' (could implement some sort of listener interface).
精彩评论