0
        EditText edText4 = (EditText) findViewById(R.id.f1text4);
        EditText edText5 = (EditText) findViewById(R.id.f1text5);
        EditText edText6 = (EditText) findViewById(R.id.f1text6);
        EditText edText7 = (EditText) findViewById(R.id.f1text7);
        EditText edText8 = (EditText) findViewById(R.id.f1text8);
        EditText edText9 = (EditText) findViewById(R.id.f1text9);
        EditText edText10 = (EditText) findViewById(R.id.f1text10);
        EditText edText11 = (EditText) findViewById(R.id.f1text11);
        EditText edText12 = (EditText) findViewById(R.id.f1text12);
        EditText edText13 = (EditText) findViewById(R.id.f1text13);
        EditText edText14 = (EditText) findViewById(R.id.f1text14);
        //........

        EditText edText65 = (EditText) findViewById(R.id.f1text65);

        EditText[] editList = {edText4, edText5, edText6, edText7, edText8, edText9, edText10, edText11, edText12, edText13, edText14...edText65};


        for (EditText view : editList){
            view.setOnFocusChangeListener(focusListener);
        }

Besides create it line by line, how to code it more efficient or better way, so I could setup listener to each EditText?

3 Answers 3

0

What you can do is.

List<EditText> editTextList = new ArrayList<>();
for (int i = 4; i <= 65; i++) {
      EditText editText = (EditText) findViewById(
               getResources().getIdentifier("f1text" + i, "id", "package_name"));
      editTextList.add(editText);
}

Which will fetch all the EditText sequentially. and do your task.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know much about android's R.id but below is what you might want to do:

List<EditText> editTextList = new ArrayList<>();

for(i = 0; i < R.id.f1text.length; i++) {
    EditText editText = (EditText) findViewById(R.id.f1text[i]);
    editText.setOnFocusChangeListener(focusListener);
    editTextList.add(editText);
}

This answer 'how to get R.id's in the int array?' you might want to use to create R.id as array.

Comments

0

Alright, figure out another way to achieve what i intend to(setup listner to each EditText in the page):

ViewGroup group = (ViewGroup)findViewById(R.id.f1entrygroup);
        for (int i = 0, count = group.getChildCount(); i < count; ++i) {
            View view = group.getChildAt(i);
            if (view instanceof EditText) {
                view.setOnFocusChangeListener(focusListener);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.