作业要求:
在第一个Activity通过两个EditText分别输入学号和姓名,然后通过数据传递,在第二个Activity上显示出刚才输入的学号和姓名。
布局要求用约束布局实现。
Activity的启动和结束
从当前页面跳到新页面,跳转代码如下:
startActivity(new Intent(源页面.this,目标页面.class));
从当前页面回到上衣页面,相当于关闭当前页面,返回代码如下:
finish();//结束当前的活动页面
代码学习部分
首先,右击MyApplication4,创建一个新的Module
右击activitystudy->new->Activity->Empty Activity,设名字为ActStartActivity
同理再new一个actfinishactivity
Activity的生命周期
各状态之间的切换过程
打开新页面的方法调用顺序为:
onCreate->onStart->onResume
关闭旧页面的方法调用顺序为:
onPause->onStop->onDestroy
Activity的启动模式
某App先后打开两个活动,此时活动栈的变动情况如下图所示。
一次结束已打开的两个活动,此时活动栈的变动情况如下图所示。
实现作业的代码
<?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>