2011-08-19 112 views
1

嗨我很新的android開發,並希望創建一個應用程序與三個頁面和主頁與按鈕導航到不同的頁面。我已經創建了不同的XML頁面,我相信我必須創建某種onclick事物,但我不確定。Android按鈕打開xml佈局

什麼Java代碼,我需要,我還有什麼需要添加到普通按鈕XML等任何人都可以請告訴我一個例子落實到自己的工作與圖片請

這是我的一個按鈕

<Button 
    android:id="@+id/information" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#fff" 
    android:background="@drawable/custombutton" 
    android:text="Information >" 
    android:textSize="18dp" 
    android:layout_marginBottom="2dp" 
    /> 

這是唯一的java文件我有(做我到這個或作出新的)

package com.techie.layout; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 
import reading.festival.guide.R; 

public class LayoutPractice extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

//makes full screen and takes away title bar 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
// 

setContentView(R.layout.linear); 
} 

} 

我其他的XML佈局被稱爲地圖,信息,和林原來的xml被稱爲線性。

我希望我已經提供了足夠的信息,有人來幫助我,我敢肯定,它很容易和IM愚蠢

歡呼

回答

3

最簡單的方法是使用在你的XML來定義的點擊處理程序屬性:

<Button 
    android:id="@+id/information" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#fff" 
    android:background="@drawable/custombutton" 
    android:text="Information >" 
    android:textSize="18dp" 
    android:layout_marginBottom="2dp" 
    android:onClick="myClickHandler" 
    /> 

通過添加此,你告訴一個Android的函數名的活動中被點擊的按鈕時調用。這個函數必須遵循一些規則。它必須有一個返回類型的void,只有一個參數是一個View。與上面的例子後,您還需要添加以下你的活動,這將是:

public void myClickHandler(View v){ 
    //handle the click here 
} 

你可能需要爲你的網站地圖,信息和linuep佈局創建單獨的活動。不要忘記在manifest.xml中聲明這些新的活動。

比方說你可以創建一個叫InformationActivity。從我們的例子中單擊按鈕啓動它,你可以這樣做:

public void myClickHandler(View v){ 
     startActivity(new Intent(this, InformationActivity.class); 
    } 

欲瞭解更多詳情,請閱讀有關在Android dev guide活動和UI。

+0

這聽起來不錯,活動的名稱寫的可能是我是誰後,但你能說得很慢,並與作爲很多信息可能,因爲你剛剛吹我的腦海,但謝謝(例如,我到底是什麼東西,即文件等) –

+0

我已經添加了一些更多的細節和一個指向Android開發指南。您應該閱讀開發指南以瞭解所有基本概念。你也可能想要拿起一本Android書。我很樂意回答您可能遇到的具體問題。 – elevine

+0

非常感謝您的幫助和非常快速的回覆,您能否確切地告訴我在哪裏創建這些活動等即時通訊仍然不確定在哪裏把這些代碼片段 –

1

你應該得到的按鈕對象到你的代碼,並添加新的ClickListener它

Button infoButton = (Button) findViewById(R.id.information); 
infoButton.setOnClickListener(new OnClickListener(){ 
    public void onClick(View view){ 
     // here is your button click logic, for example running another activity (page) 
     startActivity(new Intent(LayoutPractice.this, SiteMapActivity.class)); 
    } 
}); 

希望這有助於。 PS。代碼可能是錯誤的,從內存

+0

這是有幫助的,但是我會把這個請求放在哪裏 –

0
Intent intent = new Intent(this, NewActivity.class); 
startActivity(intent); 

其中NewActivity是你想要呈現