簡體   English   中英

同一布局中是否可以有2個具有相同功能的按鈕?

[英]Is it possible to have 2 buttons within the same layout that have the same functionality?

我的搜索清單很長。 我希望列表頂部和底部的按鈕可以“搜索”。 兩個按鈕將具有相同的功能。 我認為可以只復制並粘貼xml布局最后的第一個按鈕,但是最后的按鈕沒有功能。 如何在不向活動中添加一堆額外代碼的情況下使其工作?

<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>

您可以添加

Button
 ...
 android:onClick="functionName"

這兩個按鈕,然后在您的“ Activity創建一個函數,例如

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

然后它將知道您按下了哪個。 但是,將一個Button放在List頂部可能會更有效

如果將相同的onClickListener附加到每個對象,則它們將具有相同的功能。 您可以使用android:onClick XML屬性以XML格式執行此操作以避免代碼。

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

為2按鈕設置不同的ID,因此代碼將如下所示

<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>

並在您的活動中使用您設置的其他ID聲明按鈕,並且您可以為兩個按鈕設置相同的偵聽器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM