2014-03-30 30 views
-1

我有一個關於活動的非常初學者的問題,我剛剛開始一個新項目,並添加了一個按鈕。我想知道如何創建第二個活動(在eclipse中),然後如何使用按鈕將第一個活動鏈接到第二個活動。如何在Android應用中製作按鈕開關活動

+0

谷歌優先 –

回答

1

要打開活動中的其他活動,您應該使用Intents。從Android的文檔

教程:http://developer.android.com/guide/components/intents-filters.html

例子:

//     The context, The activity to open 
Intent intent = new Intent(this, NewActivity.class); 
// It will open the activity 
startActivity(intent); 

Intent constructorstartActivity

它會打開NewActivity活動,你應該用類名來打開替換NewActivity.class

請記住,你應該添加活動在AndroidManifest.xml


既然你這麼問,開在按鈕的活動點擊你需要使用按鈕的OnClickListenersetOnClickListener將用於設置監聽器。

// i get the reference to the button from the XML 
Button button = (Button)findViewById(R.id.button); 
// now i set the listener 
button.setOnClickListener(new View.OnClickListener() 
{ 
    @Override 
    public void onClick(View v) 
    { 
     // Here you should add the code you want to execute when the button is clicked 
     // In our case we want to open the activity 
     Intent intent = new Intent(this, NewActivity.class); 
     // It will open the activity 
     startActivity(intent); 
     // ... and stop. 
    } 
}); 

new View.OnClickListener()此行創建一個實現接口View.OnClickListener ...閱讀更多here,如果你想知道一個匿名類。

+0

嗯多一點說明會有所幫助,比如我在哪裏放這個,我該如何鏈接按鈕,我該如何設置onClick監聽器。對不起,我剛剛開始編程安卓前幾天 – user3200546

+0

好吧,我會添加如何打開按鈕點擊活動 –

+0

@ user3200546請參閱編輯。 –

相關問題