2017-07-24 49 views
0
 public class MainActivity extends FragmentActivity { 

     TextView dateView, timeView; 
     String date; 

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

      dateView = (TextView) findViewById(R.id.dateTextID); 
      timeView = (TextView) findViewById(R.id.timeTextID); 

     } 

     public void showDatePickerDialog(View v) { 
      DialogFragment newFragment = new DatePickerFragment(); 
      newFragment.show(getFragmentManager(), "datePicker"); 
     } 

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

     //*******************************************// 
     //DATE PICKER CLASS 
     public static class DatePickerFragment extends DialogFragment 
       implements DatePickerDialog.OnDateSetListener { 

      @Override 
      public Dialog onCreateDialog(Bundle savedInstanceState) { 
       // Use the current date as the default date in the picker 
       final Calendar c = Calendar.getInstance(); 
       int year = c.get(Calendar.YEAR); 
       int month = c.get(Calendar.MONTH); 
       int day = c.get(Calendar.DAY_OF_MONTH); 

       // Create a new instance of DatePickerDialog and return it 
       return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day); 
      } 

      public void onDateSet(DatePicker view, int year, int month, int day) { 
       // Do something with the date chosen by the user 
       String date =Integer.toString(day)+"/"+Integer.toString(month)+"/"+Integer.toString(yeat); 
       dateView.setText(date); 
      } 
     } 
     //*******************************************// 
     //TIME PICKER CLASS 
     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 
      } 
     } 

    } 

我想设置dateView和timeView(都是TextView)来显示用户选择的日期和时间,但问题是我不能在onDateSet和onTimeSet方法中做任何事情,因为它们是静态的。如何从日期/时间选取器对话框中获取日期和时间并显示它?

在onDateSet():

字符串日期= Integer.toString(天)+ “/” + Integer.toString(月)+ “/” + Integer.toString(yeat); dateView.setText(date);

这给出的错误是,无法从静态上下文中引用非静态字段dateView。如何解决这个问题。我寻找类似的问题,但他们都只是DatePickerFragment不是在同一个班级的日期和时间。 这里是我的布局XML:

<RelativeLayout 
    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" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="showDatePickerDialog" 
     android:text="@string/date_button_set" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <TextView 
     android:id="@+id/dateTextID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/date_selected" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="54dp" 
     android:layout_marginStart="54dp" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="showTimePickerDialog" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_alignStart="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:text="@string/time_button_set" /> 

    <TextView 
     android:id="@+id/timeTextID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button" 
     android:layout_alignLeft="@+id/dateTextID" 
     android:layout_alignStart="@+id/dateTextID" /> 

</RelativeLayout> 

回答

1

使用的接口进行通信集日期和时间返回到活动。

public class MainActivity extends FragmentActivity implements 
    DatePickerFragment.DatePickedListener, TimePickerFragment.TimePickedListener { 

    TextView dateView, timeView; 
    String date; 

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

     dateView = (TextView) findViewById(R.id.dateTextID); 
     timeView = (TextView) findViewById(R.id.timeTextID); 

    } 

    public void showDatePickerDialog(View v) { 
     DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(getFragmentManager(), "datePicker"); 
    } 

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

    @Override 
    public void onDatePicked(String date) { 
     dateView.setText(date); 
    } 

    @Override 
    public void onTimeSet(String time) { 
     timeView.setText(time); 
    } 
} 

的DatePicker类

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 

    private static final String TAG = "DatePickerFragment"; 

    private SimpleDateFormat dateFormatterEntry = new SimpleDateFormat("M/dd/yyyy"); 
    private Calendar calendar; 
    int year; 
    int month; 
    int day; 

    private DatePickedListener listener; 

    public static interface DatePickedListener { 
     void onDatePicked(String date); 
    } 

    @Override 
    @NonNull 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     Log.i(TAG, "onCreateDialog"); 

     calendar = Calendar.getInstance(); 
     year = calendar.get(Calendar.YEAR); 
     month = calendar.get(Calendar.MONTH); 
     day = calendar.get(Calendar.DAY_OF_MONTH); 

     listener = (DatePickedListener) getActivity(); 

     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 
     calendar.set(year, month, day, 0, 0); 
     String formattedDate = dateFormatterEntry.format(calendar.getTime()); 
     Log.i(TAG, formattedDate); 
     listener.onDatePicked(formattedDate); 
    } 
} 

做同样的TimePicker并添加接口

public class TimePickerFragment extends DialogFragment 
      implements TimePickerDialog.OnTimeSetListener { 

     private TimePickedListener listener; 

     public static interface TimePickedListener { 
      void onTimePicked(String time); 
     } 

     @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) { 
      listener.onDatePicked(Integer.toString(hourOfDay)); 
     } 
    } 
+0

您的回答有效!谢谢。 还有一个问题,我应该保留Date和TimePickerFragment作为不同的文件或将其放在MainActivity类 –

+0

我认为最好将DatePicker和TimePicker保存在不同的文件中以获得更好的组织性和可读性! –

0

您可以定义一个静态的TextView(我知道这会导致内存泄漏)和onDateSet方法获取日期并将其设置为TextView的

0

时间选取

final Calendar getDate = Calendar.getInstance(); 
    TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() { 
     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      getDate.set(Calendar.HOUR_OF_DAY, hourOfDay); 
      getDate.set(Calendar.MINUTE, minute); 
      SimpleDateFormat timeformat=new SimpleDateFormat("HH:mm a"); 
      timeFormat.format(getDate.getTime())); 
     } 
    }, getDate.get(Calendar.HOUR_OF_DAY), getDate.get(Calendar.MINUTE), false); 
    timePickerDialog.show() 

日期选取器

final Calendar getDate = Calendar.getInstance(); 
    DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      getDate.set(year, monthOfYear, dayOfMonth); 
     SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy"); 
       format.format(getDate.getTime()); 
     } 
    }, getDate.get(Calendar.YEAR), 
      getDate.get(Calendar.MONTH), 
      getDate.get(Calendar.DAY_OF_MONTH)); 
    datePickerDialog.show(); 
相关问题