2013-03-19 94 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> 

,并在活动声明按钮使用您设定的不同身份证,您可以将同一听众设置为两个按钮

相关问题