2011-12-27 22 views
0

我想設置一個活動,當我嘗試將一些視圖分配給.java文件中的名稱時,它告訴R.layout.bathing不有字段datePicker1,它確實有它。這是我的兩個代碼片段。我應該改變什麼才能使這個工作?R.layout的原始類型int沒有字段

bathing.java

setContentView(R.layout.bathing); 
setTitle("Please Select Bathing Options"); 
bathdate = (DatePicker)findViewById(R.layout.bathing.datePicker1); 
bathtime = (TimePicker)findViewById(R.layout.bathing.timePicker1); 
add = (Button)findViewById(R.layout.bathing.button1); 

bathing.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" 
     android:text="@string/bath_tracking" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <DatePicker 
     android:id="@+id/datePicker1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/textView3" 
     android:layout_marginTop="16dp" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/datePicker1" 
     android:layout_marginTop="51dp" 
     android:text="@string/date" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TimePicker 
     android:id="@+id/timePicker1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/datePicker1" 
     android:layout_below="@+id/datePicker1" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/timePicker1" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="50dp" 
     android:text="@string/time" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/timePicker1" 
     android:orientation="vertical" > 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Yes" /> 

    <RadioButton 
     android:id="@+id/radioButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/radioGroup1" 
     android:layout_below="@+id/radioGroup1" 
     android:text="@string/No" /> 

    </RadioGroup> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/radioGroup1" 
      android:layout_alignParentLeft="true" 
      android:layout_marginBottom="36dp" 
      android:text="@string/hair" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/timePicker1" 
      android:layout_alignParentRight="true" 
      android:layout_below="@+id/radioGroup1" 
      android:inputType="textMultiLine" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/editText1" 
      android:layout_centerHorizontal="true" 
      android:text="@string/Add" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/editText1" 
      android:layout_alignBottom="@+id/editText1" 
      android:layout_alignParentLeft="true" 
      android:text="@string/comment" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

回答

4

你,而不是

bathdate = (DatePicker)findViewById(R.layout.bathing.datePicker1); 

通過使用其ID訪問視圖對象,

bathdate = (DatePicker)findViewById(R.id.datePicker1); 

等。 ID是全球性的,它們並不特定於特定的佈局。

這並不意味着每個視圖都必須具有唯一的名稱。最重要的要知道的是,findViewById()只有在當前活動的視圖層次中搜索(您可以通過在setContentView()之前調用它來嘗試此操作 - 它將始終返回空值)。最後,這意味着ID在一個佈局文件中應該是唯一的。

在多個佈局中擁有相同的ID沒有問題。通常它甚至是有用的。考慮在res/layout/main.xml中使用正常佈局進行活動,並在res/layout-land/main.xml中使用橫向格式的不同佈局。 Android會爲您管理這些佈局。如果你給具有相同目的的不同,唯一的ID意見,你會做沿着這東西在你的代碼(僞代碼示例所示)

if(layout == portrait) 
    view = findViewById(R.id.viewid_portrait); 
else if(layout == landscape) 
    view = findViewById(R.id.viewid_landscape); 

如果兩個具有相同的ID,你會不會有衝突,但可以通過單線電話findViewById()訪問。

+0

這是否意味着給每個人一個獨特的名字的最佳做法?謝謝 – Intelwalk 2011-12-27 18:45:41

+0

@Intelwalk查看我的編輯。 :) – 2011-12-27 19:14:23

+0

感謝您的幫助! – Intelwalk 2011-12-27 22:05:44

相關問題