Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 6    Views: 60

sileliasdan
Android Fan
Profile
Posts: 9
Reg: Feb 12, 2014
Toms River
3,240
02/20/14 02:06 PM (11 years ago)

capturing user input

Hello Forum! As a new comer I've been doing a lot of reading! Today I've spent a lot of time reading about capturing user data. 99% of the information I've found has been about capturing data via the use of HTML forms or the like. I don't think this is a prefer method for my intent. I would like to simply capture three integers from the user of which will be used to yield two integer results (they may float or double - no matter) the thing is that I've been having a difficult time finding how to implement this using JAVA. I have my XML screen created already. I've included a button that on click would generate the results (actually, I would like to eliminate the button and simply update the result as the user updated one of the three fields). But can't seem to figure out the proper syntax for capturing the data. Please point me in the right direction. Thank you in advance!
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
02/20/14 10:20 PM (11 years ago)
The idea is to connect the elements in the XML file to class properties in the .java file. You'll declare each element as a property like so: private EditText userAge = null; And then assign it to the XML file in the onCreateView method like so: userAge = (EditText)thisScreensView.findViewById(R.id.userAge); Now, in whatever method you're using to manipulate the data, you can retrieve it like: userAge.getText();
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
02/21/14 01:41 AM (11 years ago)
I guess I'd want to ask 'how' do you want to process the numbers… In Java, the 'formula' for most math is pretty rote… it would be something like this: double myResult = var1 + var2; providing var1 and var2 were 'eligible' double values… so, the XML file is fine; that deals with the 'appearance' of the screen… but you'll also need a class 'java' file for the code to live in… Probably the easiest way for you to play with it would be to use the 'blank' plugin in your project, and play around with it until you get something you like… Just replace the bt_screenBlank.xml (or whatever it's called) and then test your code in BT_screenBlank.java (or whatever it's called). I can't 'exactly' remember the names, but they're close; before you download your project, be sure to add a 'blank screen' and connect it to a menu, so you can test it. Look at the basics of how things are done; check out the other plugins… look for the similarities in them… and don't be afraid to play in your own plugin… if you break something, just delete it and open a fresh copy. With respect to getting your values to/from your text box, you'll address them like other android widgets… Something like this 'might' apply… or it may be slightly different… /* double myDoubleValue; EditText myTestText = (EditText) findViewById(R.id.myTestTextBox); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public void onEditorAction(TextView v, int actionId, KeyEvent event) { myDoubleValue = Double.valueOf(myTestText.toString()); } }); */ Hope this helps a little… once you've got a direction, let us know how we can help! Cheers! -- Smug
 
sileliasdan
Android Fan
Profile
Posts: 9
Reg: Feb 12, 2014
Toms River
3,240
like
02/21/14 07:38 AM (11 years ago)
Thank you both for the feedback. Chris, I've tried using your approach: public void onClick(View view) { if (TOGGLE_ON_CLICK) { mSystemUiHider.toggle(); // Let's get to work on click String userInput = findViewById(R.id.input1); Integer iMinCode = Integer.parseInt(userInput); userInput = findViewById(R.id.input2); Integer iSpindleWidth = Integer.parseInt(userInput); userInput = findViewById(R.id.input3); Integer iWidth = Integer.parseInt(userInput); /* Now that we have our three inputs let's call * our SpindleCalc( int iMinCode, int iSpindleWidth, int iWidth) */ } else { mSystemUiHider.show(); } } However, Eclipse complains saying that userInput type should be changed to "view" BUT when I do that than it complains again saying that it's in compatible with my attempt to convert that input into an integer asking me to replace "view" back to "string" ....
 
sileliasdan
Android Fan
Profile
Posts: 9
Reg: Feb 12, 2014
Toms River
3,240
like
02/21/14 07:50 AM (11 years ago)
Ok ... so it seems I was missing the EditText widget ... now imported. :o) But it still complains about wanting to change the type of userInput back to String from EditText. public void onClick(View view) { if (TOGGLE_ON_CLICK) { mSystemUiHider.toggle(); // Let's get to work on click EditText userInput1 = (EditText)findViewById(R.id.input1); Integer iMinCode = Integer.parseInt(userInput1); EditText userInput2 = (EditText)findViewById(R.id.input2); Integer iSpindleWidth = Integer.parseInt(userInput2); EditText userInput3 = (EditText)findViewById(R.id.input3); Integer iWidth = Integer.parseInt(userInput3); /* Now that we have our three inputs let's call * our SpindleCalc( int iMinCode, int iSpindleWidth, int iWidth) */ } else { mSystemUiHider.show(); }
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
02/21/14 08:02 AM (11 years ago)
Remember that the text box in XML is an EditText object, not a string. You might want to get the size of it, the color, or something else. In this case you want the text. So, you need to use the .getText method, like so: String userInputText = userInput.getText();
 
sileliasdan
Android Fan
Profile
Posts: 9
Reg: Feb 12, 2014
Toms River
3,240
like
02/21/14 08:04 AM (11 years ago)
Ok ... so it seems I was missing the EditText widget ... now imported. :o) But it still complains about wanting to change the type of userInput back to String from EditText. public void onClick(View view) { if (TOGGLE_ON_CLICK) { mSystemUiHider.toggle(); // Let's get to work on click EditText userInput1 = (EditText)findViewById(R.id.input1); Integer iMinCode = Integer.parseInt(userInput1); EditText userInput2 = (EditText)findViewById(R.id.input2); Integer iSpindleWidth = Integer.parseInt(userInput2); EditText userInput3 = (EditText)findViewById(R.id.input3); Integer iWidth = Integer.parseInt(userInput3); /* Now that we have our three inputs let's call * our SpindleCalc( int iMinCode, int iSpindleWidth, int iWidth) */ } else { mSystemUiHider.show(); }
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.