adding two numbers from editText in first activity and displaying the answer on a second activity?
Just learning and thought this would be neat to learn.
basically getting the value from开发者_JS百科 editText1 and editText2, then by pressing an add button, be able to show the result answer on a second activity. I know this is probably very easy, but I wanted to learn more about how I can do simple math practice while learning the language. Thank you for your time if you respond.
To get the value from an EditText view, call
editText.getText()
and save it to a (String) variable.Parse the String as an Integer using
Integer.parseInt(str)
. Add your two integers together in the normal manner (var1 + var2
).To start a new Activity, create an Intent that refers to the Activity class you want
(Intent intent = new Intent(context, MyActivity.class)
.Add your sum as an extra to the intent to share it with the new activity
(intent.putExtra("sum", sumVar))
.Start the activity with
startActivity(intent)
.In the onCreate() method of your new activity, you can call
getIntent()
to retrieve the Intent that was used to start it. Then you can callintent.getIntExtra("sum")
to retrieve your sum.You can display it in a TextView with
textview.setText(String.valueOf(sum))
精彩评论