-4

in my app has 10 EditText now if i change in any one edittext its add total and show in Textview

but how i do with less code

or i do with all EditText Onchange event for all EditText

I am try to Use TextWatcher to watch all Edit Text but how i get all edittext

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
        //Do your stuff
        String text;


    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // do your stuff
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // do your stuff

    }

};

plz help

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Dharmeshsharma
  • 683
  • 1
  • 11
  • 28

1 Answers1

1

A simple way would be get the parent viewgroup which holds all the EditTexts, loop through its children, and do whatever operation:

ViewGroup editTextsContainer = (ViewGroup) findViewById(R.id.editTexts_container);
int sum = 0;
int i = edtTextsContainer.getChildCount();
for(int j=0;j<i;j++) {
    View child = editTextsContainer.getChildAt(i);
    if(child instanceof EditText) {
        sum += Integer.parseInt((EditText)child).getText());
        // handle cases where edittext text is not numbers, or set the inputType in xml to avoid
    }
}
myTextView.setText(""+sum);

Code is not tested, just giving you rough idea.

Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39
  • Tell me how you are using the textwatcher... you can refer to this link: http://stackoverflow.com/questions/8543449/how-to-use-textwatcher-in-android – Aswin Kumar Apr 19 '13 at 04:12
  • thanks i found solution its my mistake. I am not add the listener so that's why not working its working fine.. – Dharmeshsharma Apr 19 '13 at 05:00