Store the results for a math problem [message #652948] |
Mon, 07 February 2011 17:34 |
Corey Messages: 8 Registered: January 2011 |
Junior Member |
|
|
I'm new to Java lets get that on the plate. I'm coming for VB and .net where I'm comfortable and usually can drive my way to a solution.
I'm trying to build my first app for my android phone. The basics it will make some complex math calculations. I wish to find how I declare a global variable I can store values to that the program can use.
Many of these values I do not need to display
I have created some code to see how different command react. Basically I wish "newVar1" and "newVar2" where variable I could store value to and when button1 is pushed view3 would show the same results view4 shows. Now view3 always = 0.0 and view 4 is the sum of input1 and input2
package appleware.servo;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class servoactivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText input1=(EditText)findViewById(R.id.input1);
final EditText input2=(EditText)findViewById(R.id.input2);
//EditText input3=(EditText)findViewById(R.id.input3);
final TextView view1=(TextView)findViewById(R.id.view1);
final TextView view2=(TextView)findViewById(R.id.view2);
final TextView view3=(TextView)findViewById(R.id.view3);
final TextView view4=(TextView)findViewById(R.id.view4);
Button button1=(Button)findViewById(R.id.button1);
final double newVar1 = 0;
final double newVar2 = 0;
input1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
double newVar1 = new Double(input1.getText().toString());
view1.setText(Double.toString(newVar1));
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
input2.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
double newVar2 = new Double(input2.getText().toString());
view2.setText(Double.toString(newVar2));
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
double result1 = new Double(input1.getText().toString())+ new Double(input2.getText().toString());
view4.setText(Double.toString(result1));
double result2 = newVar1+ newVar2;
view3.setText(Double.toString(result2));
}
});
}
}
|
|
|
Re: Store the results for a math problem [message #652991 is a reply to message #652948] |
Mon, 07 February 2011 20:15 |
|
Corey,
You've confused this forum with a Java newcomers and/or Android
newcomers forum. It's only for answering question on getting Eclipse
installed, running and usable.
There aren't "global variables" in Java per se.
I would suggest that you work through a copy of O'Reilly Head First Java
(a fun way to learn it) and get cozy with the folks at Java Ranch
(http://www.javaranch.com). It's a great place to ask beginning Java
questions.
As for Android-specific programming questions, check out the forums
listed at
http://www.javahotchocolate.com/tutorials/android.html#suppo rt
Best of luck,
Russ
On 07-Feb-11 10:34, Corey wrote:
> I'm new to Java lets get that on the plate. I'm coming for VB and .net
> where I'm comfortable and usually can drive my way to a solution.
> I'm trying to build my first app for my android phone. The basics it
> will make some complex math calculations. I wish to find how I declare a
> global variable I can store values to that the program can use.
> Many of these values I do not need to display
> I have created some code to see how different command react. Basically I
> wish "newVar1" and "newVar2" where variable I could store value to and
> when button1 is pushed view3 would show the same results view4 shows.
> Now view3 always = 0.0 and view 4 is the sum of input1 and input2
>
> package appleware.servo;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.text.Editable;
> import android.text.TextWatcher;
> import android.view.View;
> import android.view.View.OnClickListener;
> import android.widget.Button;
> import android.widget.EditText;
> import android.widget.TextView;
>
> public class servoactivity extends Activity {
> /** Called when the activity is first created. */
> @Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.main);
> final EditText input1=(EditText)findViewById(R.id.input1);
> final EditText input2=(EditText)findViewById(R.id.input2);
> //EditText input3=(EditText)findViewById(R.id.input3);
> final TextView view1=(TextView)findViewById(R.id.view1);
> final TextView view2=(TextView)findViewById(R.id.view2);
> final TextView view3=(TextView)findViewById(R.id.view3);
> final TextView view4=(TextView)findViewById(R.id.view4);
> Button button1=(Button)findViewById(R.id.button1);
> final double newVar1 = 0;
> final double newVar2 = 0;
> input1.addTextChangedListener(new TextWatcher() {
> public void onTextChanged(CharSequence s, int start, int before, int
> count) {
> // TODO Auto-generated method stub
> double newVar1 = new Double(input1.getText().toString());
> view1.setText(Double.toString(newVar1));
> }
> public void beforeTextChanged(CharSequence s, int start, int count,
> int after) {
> // TODO Auto-generated method stub
> }
>
> public void afterTextChanged(Editable s) {
> // TODO Auto-generated method stub
> }
> });
> input2.addTextChangedListener(new TextWatcher() {
>
> public void onTextChanged(CharSequence s, int start, int before, int
> count) {
> // TODO Auto-generated method stub
> double newVar2 = new Double(input2.getText().toString());
> view2.setText(Double.toString(newVar2));
> }
> public void beforeTextChanged(CharSequence s, int start, int count,
> int after) {
> // TODO Auto-generated method stub
> }
> public void afterTextChanged(Editable s) {
> // TODO Auto-generated method stub
> }
> });
> button1.setOnClickListener(new OnClickListener() {
> public void onClick(View v) {
> // TODO Auto-generated method stub
> double result1 = new Double(input1.getText().toString())+ new
> Double(input2.getText().toString());
> view4.setText(Double.toString(result1));
>
> double result2 = newVar1+ newVar2;
> view3.setText(Double.toString(result2));
> }
> });
> }
> }
|
|
|
|
Powered by
FUDForum. Page generated in 0.08742 seconds