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>
您的回答有效!谢谢。 还有一个问题,我应该保留Date和TimePickerFragment作为不同的文件或将其放在MainActivity类 –
我认为最好将DatePicker和TimePicker保存在不同的文件中以获得更好的组织性和可读性! –