简体   繁体   English

Android:时间选择器对话框

[英]Android: Time Picker Dialog

I have been trying to create a time picker in a dialog and I get an error in the fragment class saying: 我一直试图在对话框中创建一个时间选择器,我在片段类中得到一个错误:

The method is24HourFormat(Activity) is undefined for the type DateFormat in the following line. 对于以下行中的DateFormat类型,方法is24HourFormat(Activity)未定义。

In the line 在线

DateFormat.is24HourFormat(getActivity()));

This is an example straight off the android developer website so I was not expecting any errors. 这是Android开发者网站上的一个例子,所以我没想到会有任何错误。 I have shown the code below. 我已经显示了下面的代码。

If someone could help me out on this it would be most appreciated. 如果有人可以帮助我,我将非常感激。

Thanks 谢谢

Code

package com.app.timer2;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;

import com.app.timer.R;





public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);}

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }


}

Fragment 分段

package com.app.timer2;

import java.text.DateFormat;
import java.util.Calendar;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.TimePicker;



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

XML XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical" >

        <Button
        android:id="@+id/start_time_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:padding="10dp"
        android:text="@string/start_time_button"
        android:onClick="showTimePickerDialog"  />

 </LinearLayout>

你导入了java.text.DateFormat ,你需要导入android.text.format.DateFormat而不是......你使用的是错误的类。

The reason this is happening is because you're using the wrong DateFormat. 发生这种情况的原因是因为你使用了错误的DateFormat。

You have: 你有:

import java.text.DateFormat;

When you want: 当你想要的时候:

import android.text.format.DateFormat

In summary, these two objects have the same name but they are not the same object. 总之,这两个对象具有相同的名称,但它们不是同一个对象。 So you have to specifically use the android provided one since its the only one that knows how to deal with Context in method you are calling DateFormat.is24HourFormat(getActivity())); 所以你必须专门使用android提供的一个,因为它是唯一一个知道如何处理Context的方法,你正在调用DateFormat.is24HourFormat(getActivity())); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM