2011-04-06 110 views
0

fI已經創建了兩個佈局文件。第一個是main.xml中Android佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> 
     <ImageButton android:src="@drawable/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/addBtn" ></ImageButton> 
     <ImageButton android:src="@drawable/remove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/remBtn"></ImageButton> 
    </LinearLayout> 
    <ListView android:layout_height="wrap_content" android:id="@+id/myListView" android:layout_width="match_parent" 
    android:background="#676767" > 

    </ListView>  
    <Button android:text="Add" android:id="@+id/dialogAddBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button> 
</LinearLayout> 

,第二個是dialog.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" android:orientation="vertical"> 
    <EditText android:id="@+id/dialogEditText" android:text="Enter Your Text Here" android:layout_height="wrap_content" android:layout_width="match_parent"></EditText> 
    <DatePicker android:id="@+id/datPicker" android:layout_height="wrap_content" android:layout_width="match_parent" ></DatePicker> 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> 
    <Button android:text="Add" android:id="@+id/dialogAddBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button> 
    <Button android:text="Cancel" android:id="@+id/dialogCancelBtn" android:layout_width="200px" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button> 
    </LinearLayout> 
</LinearLayout> 

如果妳仔細觀察妳比就會發現,「dialogAddbtn」是兩種佈局。現在,我如何區分java編碼中的這些按鈕。 Becoz這兩個按鈕都有相同的ID。

回答

4

添加ID標籤雙方的佈局,然後使用吹氣,做同樣的事情到這個

LayoutInflater inflater = LayoutInflater.from(this); 
inflatedView1 = inflater.inflate(R.layout.layout1,null); 
inflatedView2 = inflater.inflate(R.layout.layout2,null); 
Button button1 = (Button)inflatedView1.findViewbyId(R.id.dialogAddBtn); 
Button button2 = (Button)inflatedView2.findViewbyId(R.id.dialogAddBtn); 

正如你可以看到這兩個按鈕可以被訪問,因爲兩者在不同的家長的意見。