Capturing EditText Lost Focus

I am a long-time, self-taught, amateur VB programmer who is now trying to teach himself Java and Android simultaneously. I say this so you will know that I don’t speak the lingo well, and am very very new to these two pursuits.

I have developed an Android form that has a series of EditText boxes, the contents of each which I wish to save to an array once the user has filled it in. I have figured out how to do this if the user presses the Enter key. However, people don’t actually do that: they click on the box, type, and then click on the next element.

I VB, I could write code for the lostfocus event. But I can’t find a similar method in Java.

Finally the question: is there a way to capture when an EditText has lost focus, so I can save the typed data at that type without relying on the Enter Key?

public boolean onKey(View v, int keyCode, KeyEvent event) { 
    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)){ 
        Editable wasted=edittext_asset.getText(); 
        vehdata[vehNo][Integer.parseInt((String) edittext_asset.getTag())]=wasted.toString(); 
        return true; 
    } 
    return false; 
} 

Please remember that I am so new to this that I am often still not sure where to put code snippets to make them work (new file? oncreate method? who knows). Any guidance you can give me will be gratefully and everlastingly appreciated.

Answer

I just got done doing something similar to this. I handled it by making a for-loop that just got the contents of each edittext by id and added it’s text to an array. I did this in the onclick method since i did it all after the user clicked a button.

String ohhai;
String duh = et.getText().toString();
int number = Integer.parseInt(duh);
List<String> myCollection=new ArrayList<String>();
EditText stuff;
int editt;
String loggy;
for(int z = 0; z < number; z++){
    stuff = (EditText)findViewById(z);
    editt = stuff.getId();
    loggy = Integer.toString(editt);
    Log.e("How Many", loggy);
    ohhai = stuff.getText().toString();
    myCollection.add(ohhai);
}

String [] arr = myCollection.toArray(new String[myCollection.size()]);
String separator = "0";
StringBuffer result = new StringBuffer();
if (arr.length > 0) 
   result.append(arr[0]);
for (int h=1; h < arr.length; h++) {
    result.append(separator);
    result.append(arr[h]);
}

Leave a Reply

Your email address will not be published. Required fields are marked *