开发者

Android EditText : getting entered text

开发者 https://www.devze.com 2023-04-04 16:55 出处:网络
I want to get the after entered text.Ihave done using TextWatcher. There are some issues: For example I want to enter 32.5. In that method I want to add toSET<Product>.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号