当前位置:   article > 正文

1.简述一个Activity跳转到另一个Activity时,两个Activity生命周期方法的执行过程。2.编写一个程序,要求在第一个界面中输入两个数字,在第二个界面显示第一个界面两个数字的和。_编写一个程序,要求第一个界面中输入两个数字,在第二个界面显示第一个界面两个数字

编写一个程序,要求第一个界面中输入两个数字,在第二个界面显示第一个界面两个数字

1.简述一个Activity跳转到另一个Activity时,两个Activity生命周期方法的执行过程。

首先,我创建了一个MainActivitySecondActivity两个Activity。我在两个Activity中都加入了七个方法,并加入了打印语句。在MainAcitivity中为按钮增加事件,点击第一个界面的按钮跳转到第二个Activity。

运行程序,我们从日志信息可以看出: 启动MainActivity依次执行了onCreate()、onStart()、onResume()方法,当跳转到SecondActivity时,MainActivity会执行onPause()方法,此时SecondActivity会依次执行onCreate()、onStart()、onResume()方法。这时候MainActivity会执行onStop()方法

 

SecondActivity再次返回到MainActivity时,SecondActivity会先执行onPause()方法,然后MainActivity会依次执行onRestart()、onStart()、onResume()方法,随后SecondActivity执行onstop()方法和onDestory()方法。如果退出应用程序,则MainActivity会执行onStop()方法,然后执行onDestory()方法。

2.编写一个程序,要求在第一个界面中输入两个数字,在第二个界面显示第一个界面两个数字的和。

在activity_main.xml中布置好界面,其中输入的文本框选择的是EditText组件。布局是线性垂直布局中套用两个线性水平布局的方式。

activity_main.xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.administrator.studylife.MainActivity">
  8. <LinearLayout
  9. android:layout_width="368dp"
  10. android:layout_height="wrap_content"
  11. android:layout_marginTop="10dp"
  12. android:orientation="vertical">
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_marginLeft="10dp"
  17. android:layout_marginRight="10dp">
  18. <TextView
  19. android:id="@+id/textView2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="40dp"
  22. android:text="数字A:"
  23. android:textColor="@android:color/background_dark"
  24. android:textSize="18sp" />
  25. <EditText
  26. android:id="@+id/editText3"
  27. android:layout_width="match_parent"
  28. android:layout_height="50dp"
  29. android:layout_marginLeft="15dp"
  30. android:hint="请输入第一个数字"/>
  31. </LinearLayout>
  32. <LinearLayout
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:layout_marginLeft="10dp"
  36. android:layout_marginRight="10dp">
  37. <TextView
  38. android:id="@+id/textView3"
  39. android:layout_width="wrap_content"
  40. android:layout_height="40dp"
  41. android:text="数字B:"
  42. android:textColor="@android:color/background_dark"
  43. android:textSize="18sp" />
  44. <EditText
  45. android:id="@+id/editText4"
  46. android:layout_width="match_parent"
  47. android:layout_height="50dp"
  48. android:layout_marginLeft="15dp"
  49. android:hint="请输入第二个数字"/>
  50. </LinearLayout>
  51. </LinearLayout>
  52. <Button
  53. android:layout_width="190dp"
  54. android:layout_height="81dp"
  55. android:text="计算(A+B)"
  56. android:textSize="25dp"
  57. android:id="@+id/btnGo"
  58. app:layout_constraintBottom_toBottomOf="parent"
  59. app:layout_constraintLeft_toLeftOf="parent"
  60. app:layout_constraintRight_toRightOf="parent"
  61. app:layout_constraintTop_toTopOf="parent"
  62. app:layout_constraintHorizontal_bias="0.407"
  63. app:layout_constraintVertical_bias="0.274" />
  64. </android.support.constraint.ConstraintLayout>

在activity_second.xml中定义一个TextView负责显示数据。

activity_second.xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.administrator.studylife.SecondActivity">
  8. <LinearLayout
  9. android:layout_width="368dp"
  10. android:layout_height="wrap_content"
  11. tools:layout_editor_absoluteY="0dp"
  12. tools:layout_editor_absoluteX="8dp">
  13. <TextView
  14. android:id="@+id/show_conntent"
  15. android:layout_width="match_parent"
  16. android:layout_height="50dp"
  17. android:textSize="18sp"
  18. android:text="TextView"/>
  19. </LinearLayout>
  20. </android.support.constraint.ConstraintLayout>

给两个EditText组件和一个按钮组件添加监听事件,当点击按钮时,将数字之和的值传递到SecondActivity中,因为直时传递一个值,所以两数字相加的过程就必须在MainActivity中完成。需要考虑的是,输入的应该是个数字,而不是字符,输出的又是字符,所以需要进行String和int的转化。

       MainActivity.java代码:

  1. package com.example.administrator.studylife;
  2. import android.content.Intent;
  3. import android.os.IInterface;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import org.w3c.dom.Text;
  12. public class MainActivity extends AppCompatActivity {
  13. private EditText str1;
  14. private EditText str2;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. Log.i("Aty1" ,"执行onCreate");
  20. Button btn=(Button) findViewById(R.id.btnGo);
  21. str1=(EditText)findViewById(R.id.editText3);
  22. str2 =(EditText)findViewById(R.id.editText4);
  23. btn.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. String str = null;
  27. switch (view.getId()) {
  28. case R.id.btnGo:
  29. int i1 = Integer.parseInt(String.valueOf(str1.getText()));
  30. int i2 = Integer.parseInt(String.valueOf(str2.getText()));
  31. int sum = i1 + i2;
  32. str = Integer.toString(sum);
  33. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  34. intent.putExtra("sum", str);
  35. startActivity(intent);
  36. }
  37. }
  38. });
  39. }
  40. @Override
  41. protected void onStart() {
  42. super.onStart();
  43. Log.i("Aty1" ,"执行onStart");
  44. }
  45. @Override
  46. protected void onRestart() {
  47. super.onRestart();
  48. Log.i("Aty1" ,"执行onRestart");
  49. }
  50. @Override
  51. protected void onResume() {
  52. super.onResume();
  53. Log.i("Aty1" ,"执行onResume");
  54. }
  55. @Override
  56. protected void onPause() {
  57. super.onPause();
  58. Log.i("Aty1" ,"onPause");
  59. }
  60. @Override
  61. protected void onStop() {
  62. super.onStop();
  63. Log.i("Aty1" ,"执行onStop");
  64. }
  65. @Override
  66. protected void onDestroy() {
  67. super.onDestroy();
  68. Log.i("Aty1" ,"执行onDestroy");
  69. }
  70. }

SecondActivity.java代码:

  1. package com.example.administrator.studylife;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.widget.TextView;
  7. public class SecondActivity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_second);
  12. Log.i("Aty2" ,"执行onCreate");
  13. Intent intent=this.getIntent();
  14. String sum=intent.getStringExtra("sum");
  15. TextView show_conntent=(TextView)findViewById(R.id.show_conntent);
  16. show_conntent.setText("A+B="+sum);
  17. }
  18. protected void onStart() {
  19. super.onStart();
  20. Log.i("Aty2" ,"执行onStart");
  21. }
  22. @Override
  23. protected void onRestart() {
  24. super.onRestart();
  25. Log.i("Aty2" ,"执行onRestart");
  26. }
  27. @Override
  28. protected void onResume() {
  29. super.onResume();
  30. Log.i("Aty2" ,"执行onResume");
  31. }
  32. @Override
  33. protected void onPause() {
  34. super.onPause();
  35. Log.i("Aty2" ,"onPause");
  36. }
  37. @Override
  38. protected void onStop() {
  39. super.onStop();
  40. Log.i("Aty2" ,"执行onStop");
  41. }
  42. @Override
  43. protected void onDestroy() {
  44. super.onDestroy();
  45. Log.i("Aty2" ,"执行onDestroy");
  46. }
  47. }

运行结果:

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/242533
推荐阅读
相关标签
  

闽ICP备14008679号