2014-12-01 101 views
3

我不知道如何編寫一個方法在將開始另一個活動的類。從一個普通的類方法開始新的活動

我有一個5個按鈕的頁腳,每個按鈕都應該開始一個新的活動。我想創建一個包含5個開始活動的方法的類。

我願做這樣的事情:

Footer_buttons類:

public class Footer_buttons{ 

//Back to Home activity 
    public static void home_footer(Context context) { 

     Intent intent = new Intent(context, Home_page.class); 
     context.startActivity(intent); 
    } 
} 

在我的活動之一,我想打電話給類似的東西:

private static Context context; 
.... 
     context = this; 
.... 

    public void home_footer(View view) {  
     Footer_buttons.home_footer(context); 
    } 
+2

你有什麼問題? – tyczj 2014-12-01 19:46:33

+0

將NEW_TASK標誌添加到意圖。 – Trinimon 2014-12-01 19:51:45

+0

爲什麼不'Footer_buttons.home_footer(this);'? – Blackbelt 2014-12-01 19:56:54

回答

2

你可以指定按鈕應該以多種不同方式執行的行爲。

xml onClick屬性 首先,按鈕具有名爲onClick的xml屬性。您可以爲此屬性指定一個方法名稱:

<Button 
    android:id="@+id/btnMyButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/lbl_click_here" 
    android:onClick="goToActivity" /> 

此按鈕將調用此佈局所屬Activity的goToActivity方法。

public void goToActivity(View view) { 
Intent i = new Intent(this,NewActivity.class); 
startActivity(i); 

}

onClickListener在一個片段 下面的示例將片段的onCreateView事件期間施加onClickListener在一個片段的佈局的按鈕。

這裏是片段的XML按鈕:

<Button 
    android:id="@+id/btnMyButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/lbl_click_here" /> 

請注意,我們不再使用按鈕的onclick XML屬性。

的onClickListener是一個接口,並且可以被實現爲一個匿名類的片段類的內部:在活動 以下實例適用的onClickListener到按鈕中一個活動的佈局

View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

// Find your button in the layout. 
Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton); 

btnMyButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Intent i = newIntent(getActivity(),NewActivity.class); 
     startActivity(i); 
    } 

}); 

onClickListener在片段的onCreate事件期間。

這裏是片段的XML按鈕:

<Button 
    android:id="@+id/btnMyButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/lbl_click_here" /> 

再一次,我們不使用按鈕的onclick XML屬性。

的onClickListener接口現在實現爲匿名類的活性類的內部:

// Find your button in the layout. 
Button btnMyButton = (Button)findViewById(R.id.btnMyButton); 

btnMyButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Intent i = newIntent(this,NewActivity.class); 
     startActivity(i); 
    } 

}); 

查找在運行時

查找在運行時的XML元素的XML元素,如圖前2示例要求爲元素分配一個id:

android:id="@+id/btnMyButton" 

並且此ID爲i

R.id.btnMyButton 

當一個活動正在尋找在其佈局的元素,它可以直接調用findByView方法,如在下面::■在調用代碼中引用

Button btnMyButton = (Button)findViewById(R.id.btnMyButton); 

當片段正在尋找在其佈局的元素,它必須首先調用findViewByID對自己的看法,如下:

Button btnMyButton = (Button) rootView.findViewById(R.id.btnMyButton); 

鑄造

請注意,在這兩個示例中,findViewByID的返回值都將轉換爲聲明的類型 - 在本例中爲Button。

Button btnMyButton = (Button)... 

findViewByID默認返回一個View - View是Button的父級,代表最普通的類型。

相關問題