开发者

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.


  1. To get the value from an EditText view, call editText.getText() and save it to a (String) variable.

  2. Parse the String as an Integer using Integer.parseInt(str). Add your two integers together in the normal manner (var1 + var2).

  3. To start a new Activity, create an Intent that refers to the Activity class you want (Intent intent = new Intent(context, MyActivity.class).

  4. Add your sum as an extra to the intent to share it with the new activity (intent.putExtra("sum", sumVar)).

  5. Start the activity with startActivity(intent).

  6. 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 call intent.getIntExtra("sum") to retrieve your sum.

  7. You can display it in a TextView with textview.setText(String.valueOf(sum))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜