Get the index and ID of the selected edittext for further processing

New for android and programming in general, so I learn concepts when I go.

I have a LinearLayout to which I add edittexts dynamically.

I need to get the index and id of any edittext when it is selected or in focus.

I tried to scroll the counter to check for the selected one like this:

int count = llmain.getChildCount();
      for (int i=0; i< count; ++i) {
          if ((llmain.getChildAt(i).isSelected()) == true){
            //Do Stuff
          }

but I have no idea if it is even close, and it will only be for the index .....

Help will be greatly appreciated!

thank

EDIT: There is still no reliable way to achieve this. Example below with

if(v instanceOf EditText) {
id = v.getId();
index = ll.indexOfChild(v);

-1 , . ", " if ", , . , " instanceof" ,

if (v != null){
            id = v.getId();
            index = llmain.indexOfChild(v);

setFocusableInTouchMode (true), , , clearFocus(), EditTexts . , , EditTexts .

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&(event.getKeyCode() == 66)) // KeyEvent.* lists all the key codes u pressed
    {   
        View myView = linflater.inflate(R.layout.action, null);
        myView.setId(pos);
        pos++;
        myView.setFocusableInTouchMode(true);
        llmain.addView(myView);
        myView.requestFocus();
        View v = llmain.findFocus();

        if (v != null){
            id = v.getId();
            index = llmain.indexOfChild(v);
            Context context = getApplicationContext();
            CharSequence text = "index is:" + index + "id is:" + id;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            }


    }
    return false;   
}       

, setFocusableInTouchMode, -1 ID. ? ( ) , .....

, ? !

+3
2

, , , EditText, , EditTexts:

LinearLayout llMain = (LinearLayout)findViewById(R.id.llmain);
EditText editText = new EditText(this);
//0-based index, so get the number of current views, and use it for the next
editText.setId(llMain.getChildCount());
llMain.addView(editText);

, , - (onTouch, onFocus, - ):

@Override
public void onTouch(View v, MotionEvent ev) {
    int indexAndId = v.getId();
}

:

LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
int index, id;

//finds the currently focused View within the ViewGroup
View v = ll.findFocus();

if(v instanceOf EditText) {
    id = v.getId();
    index = ll.indexOfChild(v);
}
+5

It seems like this will work for me, have you tested it and it is not working?

llmain.getChildAt(i).getId(); will return you the view id.

+1
source

All Articles