獨立的Activity,獨立意思是說,單純的切換至下各Activity(不包括資料)

android中 獨立的Activity是如何切換的呢?


其實很簡單需做以下三件事

1.新增一個java (在src/ 下面)  

2.新增一個.xml (在res/layou/ 下面)

3.在AndroidManifest.xml (裡面加入一段敘述)

 

1.新增java

首先在專案目錄上點右鍵,選擇「New->Class」選項,會出現一個New Java Class的視窗

在Package那爛的右邊Browse點取,選取我們的路徑(一開始我們所創的路徑)

在Name輸入新的檔案名(注意第一個字母需大寫)

接下來在Superclass那欄右手邊的Browse點開

在Choose a type 輸入activity 將會出現Activity-android.app選取 按OK  Finish 。

 

2.新增.xml

首先在專案目錄上點右鍵,選擇「New->Other->Android->Android XML File」選項

會出現New Android XML File 的視窗

首先選取你的Project  ,File輸入你的檔案名稱(注意後面需+.xml    EX:test.xml)

在下面選取Layout  在點Finish  。

 

3.AndroidManifest.xml 

在我們左邊Navigator(預設的位置)  專案下會有一個名為AndroidManifest.xml 的檔案

雙擊開啟它,在點選下面AndroidManifest.xml模式,下入下面這段敘述

<activity android:name="form1"></activity>

如下圖所示

圖片2.png 

 

在主要的main.java 程式如下 利用 Intent 來做Activity切換


package ksu.dcc.formexchange;


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;


public class formExchange extends Activity {

OnClickListener welcomeClick = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        welcomeClick = new OnClickListener()
        {
public void onClick(View v) 
{
Intent intent3=new Intent();
intent3.setClass(formExchange.this,form1.class);
startActivity(intent3);

}
        };
        
        Button welcomeBtn=(Button)findViewById(R.id.Button01);
        welcomeBtn.setOnClickListener(welcomeClick);
        
    }
}

main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button 
android:text="welcome"
  android:id="@+id/Button01" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content">
 </Button>
</LinearLayout>

 

新增的Activity.java


package ksu.dcc.formexchange;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class form1 extends Activity {

OnClickListener backClick = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form1main);
        
        backClick = new OnClickListener()
        {
public void onClick(View v) 
{
finish();
}
        };
        
        Button backBtn=(Button)findViewById(R.id.Button02);
        backBtn.setOnClickListener(backClick);
        
    }
}

 

新增.xml

 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  
<TextView 
android:text="This is form1" 
android:id="@+id/TextView02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</TextView>


<Button 
android:text="Back" 
android:id="@+id/Button02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>
</LinearLayout>

 

 

 

arrow
arrow
    全站熱搜

    東勢厝滴yang 發表在 痞客邦 留言(0) 人氣()