1. 程式人生 > >兩個Activity切換例子(測試通過)

兩個Activity切換例子(測試通過)

1.在專案中建立main1.xml和Actvity01.java.

2.在專案中新建一個main2.xml和Activity02.java.

3.二個佈局檔案中都定義了一個TextView和Button。

Activity01

package com.activitytest;

/**
 * Created by oxiao on 2016/10/24.
 */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class 
Activity01 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* 設定顯示main.xml佈局 */ Button button = (Button)this.findViewById(R.id.button1); button.setOnClickListener(new Button.OnClickListener(){ public void
onClick(View v){ //新建一個Intent Intent intent = new Intent(); //intent.putExtra("name","Yaoli"); //制定intent要啟動的類 intent.setClass(Activity01.this, Activity02.class); //啟動一個新的Activity startActivity(intent); //關閉當前的 Activity01.this.finish(); } }); } }

Activity02

package com.activitytest;

/**
 * Created by oxiao on 2016/10/24.
 */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity02 extends Activity{           /*savedInstanceState 儲存當前Activity的狀態資訊。*/
    //private TextView tv1;
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);         //呼叫父類的onCreate建構函式)
                                                    /* 設定顯示main2.xml佈局 */
setContentView(R.layout.main2);
                                                    /* findViewById(R.id.button2)取得佈局main.xml中的button2 */
Button button = (Button) findViewById(R.id.button2);
                                                    /* 監聽button的事件資訊 */
button.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                                                    /* 新建一個Intent物件 */
Intent intent = new Intent();
                /* 指定intent要啟動的類 */
                //String name=intent.getStringExtra("name");
                //tv1.setText("activity01傳過來的值為:"+name);
intent.setClass(Activity02.this, Activity01.class);
                                                    /* 啟動一個新的Activity */
startActivity(intent);
                                                    /* 關閉當前的Activity */
Activity02.this.finish();
            }

        });
    }
}

strings.xml

<resources>
    <string name="app_name">activitytest</string>
    <string name="hello">第一個Activity</string>
    <string name="hello2">第二個Activity</string>
</resources>


main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
    <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x = "120dp"
android:layout_y = "90dp"
android:text ="切換"
android:id="@+id/button1"
>
    </Button>
</LinearLayout>

main2.xml

<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello2"
/>

    <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x = "120px"
android:layout_y = "90px"
android:text ="切換"
android:id="@+id/button2"
>
    </Button>

</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activitytest"
android:versionCode="1"
android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
        <activity
android:name=".Activity01"
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="Activity02">
        </activity>


    </application>

</manifest>

程式碼連結:

https://github.com/yaolixiao003/apptest.git