New to android and programming in general, so learning concepts as I go.
I have a LinearLayout to which I am adding edittexts dynamically.
I need to be able to get the Index AND Id of any edittext when it is selected or in focus.
I have tried looping through a child count to check for selected like so:
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 its even close, and that would be only for index.....
Help would be greatly appreciated!
Thanks
EDIT: still dont have a reliable way to achieve this. The example below with the
if(v instanceOf EditText) {
id = v.getId();
index = ll.indexOfChild(v);
returns a -1 for index and a ten digit number for the id, however, i am assigning an id on creation. ?The odd thing is that the code inside the "if" is running, so it at least thinks it has the focused view. Now, if I change the "instanceof" to a null check like
if (v != null){
id = v.getId();
index = llmain.indexOfChild(v);
AND I add a setFocusableInTouchMode(true), I get a proper return, however, it then acts like i called clearFocus(), because none of the EditTexts are focused. Here is my full proof of concept code that returns the correct values but no longer lets the EditTexts actually have focus.
@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;
}
This returns the correct values unless I comment the setFocusableInTouchMode line, then it goes back to the odd -1 for index and ten digit for ID. What am I doing wrong? going to have to be a good (and working) answer to get over half of my rep.....
So nobody has a solution for this? its still driving me nuts!
thanks again
My initial thought would be to set the ID equal to the index when adding an EditText, assuming only EditTexts will be in this particular layout:
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);
Then, to retrieve the info, put a check into a listener of some sort (onTouch, onFocus, something like that):
@Override
public void onTouch(View v, MotionEvent ev) {
int indexAndId = v.getId();
}
Give this a shot:
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);
}
That seems like it would work to me, have you tested it and it isn't working?
llmain.getChildAt(i).getId();
will return the views ID to you.
精彩评论