3

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.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
Ed Preston
  • 33
  • 1
  • 6
  • Please show us some code, so we know you've tried, and we can add to it. – Dhaivat Pandya Jun 01 '11 at 04:30
  • 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; } – Ed Preston Jun 01 '11 at 04:42
  • woah. Add a code tag into the question. I"ll do it for you. – Dhaivat Pandya Jun 01 '11 at 05:04

2 Answers2

6

Let's go by parts:

  • "I could write code for the lostfocus event. But I can't find a similar method in Java.": Take a look at the OnFocusChangeListener API
  • "is there a way to capture when an EditText has lost focus": Yes, using the listener above and abstract void onFocusChange(View v, boolean hasFocus)
  • "Please remember that I am so new to this": we've been all new here at some point :) If you are respectful, and give back to the community (either by answers, or simply voting up/down and accepting answers), there's nothing to worry.

And then some:

I'm a self-learner as you, so I feel your pain right now, and I'd like to give you some advise now that you're starting with android: You added two tags: and . Coding in java for a desktop is quite different than coding for an android device, so please take that into consideration. Are you really going to code in java or just in android? Besides some obvious differences in the API, the main difference is in the user interaction. Most java programs expect a full keyboard and a static screen, while android apps rely on touches, and that is a key difference.

Touch is important because usually there is no lost focus without the focus gain on some other View. A typical EditText will be filled with a finger on an IME and that View will maintain the focus until the same finger will touches another View (perhaps an EditText, perhaps a Button). There are usually no clicks outside the "window".

On the other hand, in an android device you expect a single user, rather than an unknown amount of users in a desktop. Thus, saving the data and retrieving it is handled quite differently (see, for instance, Saving Android Activity state using Save Instance State)

You can choose to capture the gain/loss of focus to perform an action, or wait until a button is clicked, or even use other methods (such a TextWatcher to save the data on every character input). So, you have plenty of options to design your app. The hard part is to design smartly.

Good luck!

Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • Right now I'm focusing on programming for Android, but I am also trying to learn Java sufficfiently to replace VB for my desktop apps. I am a safety professional by trade, so I want desktop programs for data manipulation and android apps for fieldwork. – Ed Preston Jun 01 '11 at 07:20
  • 1
    The onfocuschangelistener appears to be what I want, but I now have a new glitch: It works fine when I click on another EditText, but I can't get the EditText to release focus when I click on a spinner. I've set the spinners' focusable and focusableontouch properties to true, but when I click, the onfocuschange method is not called (I set a toast in it so I could check). – Ed Preston Jun 01 '11 at 19:38
1

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]);
}
Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
DustinRiley
  • 555
  • 6
  • 15