2013-03-19 103 views
0

我有一個很長的名單與搜索選項。我想要列表頂部和底部的按鈕「搜索」。兩個按鈕都具有相同的功能。我認爲只需在xml佈局中複製並超過第一個按鈕,但最後的按鈕沒有功能。如何在不在活動中添加一堆額外代碼的情況下使其工作?是否有可能在同一個佈局中擁有2個具有相同功能的按鈕?

<ScrollView 
    android:layout_width= "fill_parent" 
    android:layout_height= "fill_parent" > 

<LinearLayout 
    android:id="@+id/LinearLayout02" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

<Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/save_search_options" 
    android:text="Search" android:textSize="25dip"></Button> 

<!--search options in between--> 


<Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/save_search_options" 
    android:text="Search" android:textSize="25dip"></Button> 

</LinearLayout> 
</ScrollView> 

回答

2

你可以只添加

Button 
... 
android:onClick="functionName" 

到兩個按鈕然後在Activity

public void functionName(View v) 
{ 
    //some logic 
} 

那麼它會知道您按創建功能。但是,它可能是更有效地只保留一個Button上的List

1

如果將相同onClickListener每個,那麼他們將具有相同的功能。您可以使用XML來執行此操作,以避免使用 XML屬性的代碼。

0

你有兩個相同ID的按鈕。重命名其中一個並將偵聽器分配給它們兩個。

0

頂端設置不同的ID爲2按鈕,該代碼會是這樣

<ScrollView 
    android:layout_width= "fill_parent" 
    android:layout_height= "fill_parent" > 

<LinearLayout 
    android:id="@+id/LinearLayout02" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

<Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/top_save_search_options" 
    android:text="Search" android:textSize="25dip"></Button> 

<!--search options in between--> 


<Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/bottom_save_search_options" 
    android:text="Search" android:textSize="25dip"></Button> 

</LinearLayout> 
</ScrollView> 

,並在活動聲明按鈕使用您設定的不同身份證,您可以將同一聽衆設置爲兩個按鈕

相關問題