- 使用Activity的setContentView方法做畫面切換
- 使用Intent類別啟動新的Activity做畫面切換
- 類別檔程式碼
package com.example.app_fir;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {Button b1;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);b1=(Button)this.findViewById(R.id.button1);b1.setOnClickListener(new OnClickListener(){public void onClick(View arg0) {setContentView(R.layout.second_app);}});}}
- 第一個UI的xml檔
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="16dp"android:layout_marginTop="28dp"android:text="請點我!!" /></RelativeLayout>
- 第二個UI的xml檔
<?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/textView1"
android:layout_width="284dp"
android:layout_height="wrap_content"android:text="@string/hello_world"android:textSize="50sp" /></LinearLayout>
- strings的xml檔
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app_fir</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
- Activity的xml檔
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app_fir"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.app_fir.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></application></manifest>
第二種實作(以下所有的檔案都要儲存)
- 第一個類別檔程式碼
package com.example.app_fir;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {Button b1;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);b1=(Button)this.findViewById(R.id.button1);b1.setOnClickListener(new OnClickListener(){public void onClick(View arg0) {Intent it=new Intent();it.setClass(MainActivity.this, second_app.class);MainActivity.this.startActivity(it);}});}}
- 第二個類別檔程式碼
package com.example.app_fir;
import android.app.Activity;
import android.os.Bundle;
public class second_app extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_app);
}
}
- 第一個UI的xml檔
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="16dp"android:layout_marginTop="28dp"android:text="請點我!!" /></RelativeLayout>
- 第二個UI的xml檔
<?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/textView1"
android:layout_width="284dp"
android:layout_height="wrap_content"android:text="@string/hello_world"android:textSize="50sp" /></LinearLayout>
- strings的xml檔
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app_fir</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
- Activity的xml檔
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app_fir"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.app_fir.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="second_app"></activity></application></manifest>
沒有留言:
張貼留言