gpt4 book ai didi

java - 如何从日期/时间选择器对话框获取日期和时间并显示它?

转载 作者:行者123 更新时间:2023-12-01 08:45:32 26 4
gpt4 key购买 nike

        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(年); dateView.setText(日期);

这会产生错误,无法从静态上下文引用非静态字段 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>

最佳答案

使用界面将设置的日期和时间传达回 Activity 。

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

关于java - 如何从日期/时间选择器对话框获取日期和时间并显示它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45283857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com