我是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中的错误也很少。 请帮帮我。 感谢
答案 0 :(得分: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();