3

我从Android Developers获取了原始的 TimePicker-Source

并将其粘贴到 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()”,它已被贬低。

4

4 回答 4

8

提供的示例隐含地假设您将该代码添加到另一个内部ActivityFragment作为内部类。如果那不是您正在做的事情,请static从中删除。请注意,尽管DialogFragment它在外部类中的通信有点令人厌烦(您必须使用接口来来回通信)。

关于您的第二个问题,要显示对话框,请使用以下内容:

DialogFragment dialog = new MyDialogFragment();
dialog.show(getFragmentManager(), "MyDialog");
于 2013-10-08T21:13:39.470 回答
1

你试图将上面的内容与activity类减速平行粘贴,实际上上面的代码应该是一个内部类。

public class NewCaseEntry extends AppCompatActivity {}
public static class TimePickerFragment extends ... {}

这是错误的,应该是

public class NewCaseEntry extends AppCompatActivity {
    public static class TimePickerFragment extends ... {}
}
于 2017-06-01T07:08:38.977 回答
0

首先,感谢卡斯拉!

我还发现,你必须把它放在一个对话框片段中。但这些来自 API 11 及更高版本(我的将支持 API 5)。可以通过在 Android Studio 中安装兼容性包来使用它们。所以我等待贬值,也许在 API 20 中我的应用程序已经过时了 :-)

于 2013-10-11T10:17:00.450 回答
0

它应该用作带有静态字的内部类,如下所示:

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
    }
}

}

如果你想把它作为一个单独的类,你可以在没有静态词的情况下使用它,如下所示:

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
    }
}
于 2019-08-07T11:25:16.403 回答