2013-02-08 53 views
1

我在Android的編程初學者。我想在一個片段實現與時報廣場的片段

public class CalendarFragment extends Fragment { 

private static final String TAG = "CalendarFragment"; 

View viewer; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     viewer = (View) inflater.inflate(R.layout.cal, container, 
       false); 
    Calendar nextYear = Calendar.getInstance(); 
    nextYear.add(Calendar.YEAR, 1); 

    final CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar); 
    calendar.init(new Date(), new Date(), nextYear.getTime()); 

    Button b = (Button) findViewById(R.id.done_button); 
    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 
     Log.d(TAG, "Selected time in millis: " + calendar.getSelectedDate().getTime()); 
     String toast = "Selected: " + calendar.getSelectedDate().getTime(); 
     Toast.makeText(CalendarFragment.this, toast, LENGTH_SHORT).show(); 
     } 
    }); 
     return viewer; 

    } 

cal.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <com.squareup.timessquare.CalendarPickerView 
     android:id="@+id/calendar" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:paddingBottom="16dp" 
     android:scrollbarStyle="outsideOverlay" 
     android:clipToPadding="false" 
     android:background="#FFFFFF" 
     /> 

    <Button 
     android:id="@+id/done_button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Done" 
     /> 

</LinearLayout> 

我認爲問題是在XML中,也用Java幾個錯誤添加日曆(https://github.com/square/android-times-square)。 請幫我一把。 感謝

+0

你看到什麼錯誤?它編譯正確,但崩潰?如果是的話發佈logcat。或者它甚至沒有編譯?如果是這樣,則發佈列出的錯誤以及它們出現在哪些行上。 – FoamyGuy 2013-02-08 17:35:37

+0

它顯示「的方法findViewById(INT)是未定義的類型CalendarFragment」和「在類型吐司的方法makeText(上下文,CharSequence的,INT)是不適用的參數(CalendarFragment,字符串,整數)」。 – 2013-02-08 17:45:26

回答

3

我已經更新了您的片段代碼。

public class CalendarFragment extends Fragment { 

private static final String TAG = "CalendarFragment"; 

View viewer; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     viewer = (View) inflater.inflate(R.layout.cal, container, 
       false); 
    Calendar nextYear = Calendar.getInstance(); 
    nextYear.add(Calendar.YEAR, 1); 

    final CalendarPickerView calendar = (CalendarPickerView) viewer.findViewById(R.id.calendar); 
    calendar.init(new Date(), new Date(), nextYear.getTime()); 

    Button b = (Button) viewer.findViewById(R.id.done_button); 
    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 
     Log.d(TAG, "Selected time in millis: " + calendar.getSelectedDate().getTime()); 
     String toast = "Selected: " + calendar.getSelectedDate().getTime(); 
     Toast.makeText(getActivity(), toast, LENGTH_SHORT).show(); 
     } 
    }); 
     return viewer; 

    } 

具體地說,

final CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar); 

需要被 最終CalendarPickerView日曆=(CalendarPickerView)viewer.findViewById(R.id.calendar);

和,

Button b = (Button) findViewById(R.id.done_button); 

需要被

Button b = (Button) viewer.findViewById(R.id.done_button); 

以及最後,

Toast.makeText(CalendarFragment.this, toast, LENGTH_SHORT).show(); 

需要被

Toast.makeText(getActivity(), toast, LENGTH_SHORT).show(); 
+1

謝謝..它的工作.. – 2013-02-08 18:35:35