弹出DatePicker使用DialogFragment并将选择保存到TextView或EditText

时间:2017-04-21 22:24:13

标签: java android datepicker calendar popup

我将此部分写作完整新人的指南,并部分地询问最佳实践。经过一段时间尝试实现上述问题,并使用现在的void showDialog方法主要为DatePickerDialogs查找指南后,我最终将一个可行的解决方案拼凑在一起,但大多数其他类似的答案都假定了大量的先前知识。

我使用了:一个新的DatePickerFragment.java类,一个Activity java类和一个XML布局。

目的是让TextView显示当前日期加上一周,旁边有一个按钮,弹出日历以选择新日期,这将填充带有字符串日期的TextView和隐藏查看该日期为长。

我最终得到了以下代码:

public class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

// Set current date to default date in date 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);
   }
}

这与此处列出的官方用法略有不同:https://developer.android.com/guide/topics/ui/controls/pickers.html

而不是implements DatePickerDialog.OnDateSetListener在DatePickerFragment中,我把它放在下面的NewTaskActivity中:

public class NewTaskActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
private TextView text_date_picker;
private TextView text_date_picker_hidden;

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

    text_date_picker = (TextView) findViewById(R.id.text_date_picker);
    text_date_picker_hidden = (TextView) findViewById(R.id.text_date_picker_hidden);

//get current date to set as default displayed date
    long datemilliseconds = System.currentTimeMillis()+ (86400 * 7 * 1000);
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    Date resultdate = new Date(datemilliseconds);
    String formattedDate = sdf.format(resultdate);
// set default date to one week after todays date
    text_date_picker.setText(formattedDate);
    text_date_picker_hidden.setText(String.valueOf(datemilliseconds));
}
//when pressed the button creates the pop up calendar
public void onNewTaskDateClick(View view){
    DialogFragment picker = new DatePickerFragment();
    picker.show(getFragmentManager(), "datePicker");
}
//the date selected on the calendar changes the text stored in the TextView
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    Calendar c = Calendar.getInstance();
    c.set(year, month, day);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDate = sdf.format(c.getTime());

    text_date_picker.setText(formattedDate);
    text_date_picker_hidden.setText(String.valueOf(c.getTimeInMillis()));
}

这些操作的XML布局代码如下所示:

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/text_date_picker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />

            <TextView
                android:id="@+id/text_date_picker_hidden"
                android:text=""
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <Button
                android:id="@+id/button_choose_task_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onNewTaskDateClick"
                android:text="Change Exipry Date" />
        </LinearLayout>

我不确定最佳做法是改变implements DatePickerDialog.OnDateSetListener的位置,而且对Android来说是全新的,所以可能在其他地方出错了!请随时告诉我,或以此为指导。

0 个答案:

没有答案