簡體   English   中英

android表中每個單元格的唯一ID

[英]unique id to each cell in android table

我有一個帶有按鈕作為表元素的android表。 我想為每個單元格(按鈕)分配一個唯一的ID,這樣當我單擊特定的單元格(按鈕)時,它應該返回各自的ID。 請幫我怎么做。 示例代碼會很棒。 謝謝。

在xml中使用

 android:id ="@+id/your_id"

或以編程方式

yourelement.setId(yourID);

  button.setId(01);

其中button是Button類的對象

如果您在xml中有硬編碼的按鈕,請在xml本身中設置id,如下所示:

     <Button
        android:id="@+id/btn_row_col"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="button1"
        />

如果要動態添加,則:

 Button newBtn=new Button();
   newBtn.setText("button");
   newBtn.setId(yourId);  //here setid to your button

暫無
暫無

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

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