并将其粘贴到Android Studio中的新类中。 Android Studio会抛出错误“修改程序'静态',此处不允许”。怎么了?这是来源:
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
}
}
顺便说一下:有没有一种简单的方法可以在活动中使用时间戳?这里几乎所有的例子都使用“showDialog()”,这是一个被删除的。
答案 0 :(得分:8)
提供的示例隐式假设您将该代码添加到另一个Activity
或Fragment
内部作为内部类。如果那不是您正在做的事情,请从中删除static
。请注意,DialogFragment
之间在外部类中进行通信时会有点烦人(您必须使用接口来回通信)。
关于第二个问题,要显示对话框,请使用以下内容:
DialogFragment dialog = new MyDialogFragment();
dialog.show(getFragmentManager(), "MyDialog");
答案 1 :(得分:1)
您试图将上述内容与活动类减速并行粘贴,实际上上面的代码应该是内部类。
public class NewCaseEntry extends AppCompatActivity {}
public static class TimePickerFragment extends ... {}
这是错的,应该是
public class NewCaseEntry extends AppCompatActivity {
public static class TimePickerFragment extends ... {}
}
答案 2 :(得分:0)
首先,感谢Kasra!
我还发现,你必须将它放在一个对话框片段中。但这些来自API 11及更高版本(我的将支持API 5)。 可以通过在Android Studio中安装兼容包来使用它们。所以我等待去除,可能在API 20我的应用已经过时了: - )
答案 3 :(得分:0)
它应该用作内部类,并带有 static 词,如下所示:
public class yourclassname extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_input );
}
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
}
}
}
如果要将其作为单独的类,则可以使用它而无需使用 static 字词,如下所示:
public 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
}
}