此链接显示如何使用DialogFragment创建时间选择器 http://developer.android.com/guide/topics/ui/controls/pickers.html#TimePicker
显示的示例中是否可能存在拼写错误?当我在Eclipse中的新类中复制并粘贴示例时,它表示 static 是非法修饰符。我究竟做错了什么? 这就是我在Eclipse中所拥有的:
package com.example.symptommanagement;
import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
顺便说一句,如果我删除静态修饰符,代码就可以运行。
答案 0 :(得分:1)
我认为你为Fragment创建了一个简单的分离类。见this SO问题:
片段必须是常规Java类或静态内部类,并且它们需要具有公共零参数构造函数。
我认为,Google Developer网站上的所有示例都是静态内部类。
在Java中,只有嵌套类可以是static
个类,请参阅:Why are you not able to declare a class as static in Java?