Activity learning - the second homework for Android

Work requirements:

In the first Activity, enter the student number and name through two EditTexts, and then pass the data, and display the student number and name just entered on the second Activity.

Layout requirements are implemented using constraint layout.

 Activity start and end

To jump from the current page to a new page, the jump code is as follows:

startActivity(new Intent(source page.this, target page.class));

Going back to the tops page from the current page is equivalent to closing the current page, and the return code is as follows:

finish();//End the current active page

code learning part 

First, right click on MyApplication4 and create a new Module

Right-click activitystudy->new->Activity->Empty Activity, set the name to ActStartActivity 

In the same way, create a new actfinishactivity

Activity life cycle

 

Switching process between states

The sequence of method calls to open a new page is:

  onCreate->onStart->onResume

The method call sequence for closing the old page is:

onPause->onStop->onDestroy

Activity's startup mode

An App opens two activities one after another. At this time, the change of the activity stack is shown in the figure below.

 

End the two opened activities at one time, and the changes of the activity stack at this time are shown in the figure below. 

Code that implements the job

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    <TextView
        android:id="@+id/tv_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="参数传递作业">
    </TextView>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入您的姓名:" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入您的学号:" />

    <EditText
        android:id="@+id/et_height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" />
    <Button
        android:id="@+id/btn_act_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第二个Activity">
    </Button>


</LinearLayout>

 

package com.example.activitystudy;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class ActStartActivity extends Activity {

    private static final String TAG="ning";

    private Button btn_tursec;
    private EditText et_name,et_height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_start);
        //使用findViewById()获取Button对象
        btn_tursec=(Button) findViewById(R.id.btn_act_next);
        et_name=(EditText) findViewById(R.id.et_name);
        et_height=(EditText) findViewById(R.id.et_height);
        btn_tursec.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String name=et_name.getText().toString();
                String height=et_height.getText().toString();
                Intent intent=new Intent();
                intent.setClass(ActStartActivity.this, Actfinishactivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("name", name);
                bundle.putString("height", height);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                Bundle bundle=data.getExtras();
                //String returnValue=bundle.getString("returnStr");
                //et_name.setText(returnValue);
                break;
            default:
                break;
        }
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <Button
        android:id="@+id/btn_finish"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="完成">
    </Button>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按完成键可关闭当前界面,返回上一界面">
    </TextView>

</LinearLayout>

 

package com.example.activitystudy;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Actfinishactivity extends Activity {
    Intent intent;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_actfinishactivity);
        intent=this.getIntent();
        bundle=this.getIntent().getExtras();
        String name=bundle.getString("name");
        String height=bundle.getString("height");
        Button firact = (Button) findViewById(R.id.btn_finish);
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText("您的输入信息为 "+"\n 姓名: "+name+"\n学号:"+height);
        firact.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                intent.putExtras(bundle);
                Actfinishactivity.this.setResult(RESULT_OK,intent);
                Actfinishactivity.this.finish();
            }
        });
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication4">
        <activity
            android:name=".Actfinishactivity"
            android:exported="false" />
        <activity
            android:name=".ActStartActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

Guess you like

Origin blog.csdn.net/qq_52045638/article/details/129660337