activity就是一个新的布局页面,MainActivity就是继承Activity显示在我们手机的页面,初学的我们都是在一个页面上做文章,但是学过一段时间后,页面已经远远不能满足我们了,页面跳转就是我们新建二个FActivitty和SActivity之间相互跳转,都是新建一个类,继承Activity,创建布局
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
;在我们新建的类之下创建可视化布局factivity.xml文件,在类里面引入我们的xlm文件 setContentView(R.layout.factivity);,这样我们在xlm文件布局之后就可以显示在手机页面上了,但是需要注意的是每当我们新建个类,就要在Androidmainfest里面注册。
有 <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
标签的类就有优先显示在手机页面的权利。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wuzuo.app2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".FActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SActivity">
</activity>
</application>
</manifest>
FActivity.java
package com.example.wuzuo.app2;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by wuzuo on 2016/7/21.
*/
public class FActivity extends Activity {
private Button button;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
context=this;
button= (Button) findViewById(R.id.button1_first);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(context,SActivity.class);
startActivity(intent);
}
});
}
}
SActivity.java
package com.example.wuzuo.app2;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by wuzuo on 2016/7/21.
*/
public class SActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
}
}
factivity.xlm
<?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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种启动方式"
android:id="@+id/button1_first" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种启动方式"
android:id="@+id/button2_second" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="把第二个页面回传的数据显示出来"
android:id="@+id/textView1"
/>
</LinearLayout>
sactivity.xlm
<?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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button1" />
</LinearLayout>
点击第一个按钮