1

我是Android编程的初学者。我正在尝试在片段中添加日历( https://github.com/square/android-times-square )

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中也很少出错。请帮帮我。谢谢

4

1 回答 1

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

需要为final CalendarPickerView calendar = (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();
于 2013-02-08T18:26:44.050 回答