启动第二个Activity
activity_main.xml文件:
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动第二个Activilty" /> </RelativeLayout>
创建activity_second.xml文件:
SecondActivity.java:
package com.example.z1178.test;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;public class SecondActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); }}
MainActivity.java文件:
package com.example.z1178.test;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { private static final String TAG="debug"; private Button myButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButton=(Button)findViewById(R.id.myButton); buttonClickListener listener=new buttonClickListener(); myButton.setOnClickListener(listener); } class buttonClickListener implements View.OnClickListener{ @Override public void onClick(View v) { Intent intent=new Intent(); //setClass的第一个參数为Context对象(Activity为其子类),第二个參数为要启动的Activity intent.setClass(MainActivity.this,SecondActivity.class); startActivity(intent); } }}
AndroidManifest.xml文件:
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.z1178.test" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="@string/title_activity_second" > </activity> </application> </manifest>
对于back stack。其维护的Activity对象,我们须要注意的是,其总是显示顶端的Activity,当我们按回退键的时候,back stack会依照与进去栈的相反顺序依次显示相应的Activity。
posted on 2017-07-07 09:28 阅读( ...) 评论( ...)