【问题标题】:How to Get Date from DatePicker When Button Clicked单击按钮时如何从 DatePicker 获取日期
【发布时间】:2016-03-03 01:19:37
【问题描述】:

我是 android 新手,正在尝试将 DatePicker 用于我的应用程序。它存在于 date.xml 文件中。我使用 setView(R.layput.date) 命令制作了 AlertDialog 框以显示 DatePicker。当我单击 EditText 时会出现 AlertDialog 框。现在,当我单击警报对话框的保存按钮时,我需要从 DatePicker 将日期更改为字符串。我该怎么做?

这是一组date.xml文件的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="center_horizontal"></LinearLayout>

    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker"
        android:layout_gravity="center_horizontal"
        android:layoutMode="clipBounds" />
</LinearLayout>

这是一组代码AlertDialog

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
final EditText edt = (EditText)findViewById(R.id.invo_date);
edt.setText(dateFormat.format(date));
edt.setInputType(0);
edt.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (!one){
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);

            AlertDialog.Builder alt = new AlertDialog.Builder(CreateInvoice.this);
            alt.setView(R.layout.date);
            alt.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    one = false;
                }
            });
            alt.setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // I need to get date aftre set Date form DatePicker to srting when I click Save button.

                }
            });
            alt.setCancelable(false);
            alt.show();
            one = true;
        }
        return true;
    }
});

【问题讨论】:

标签: android datepicker


【解决方案1】:

试试下面的代码:

int   day  = datePicker.getDayOfMonth();
int   month= datePicker.getMonth() + 1;
int   year = datePicker.getYear();

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String formatedDate = sdf.format(new Date(year, month, day));

【讨论】:

  • @bhargav 感谢您的帮助.. 但我有问题如何在另一个布局中访问 DatePicker?因为这个 DataPicker 存在于另一个布局文件中,并且它出现在 AlertDialog 框中。
  • 您的日期选择器确实存在于哪个文件中?
  • R.layout.date 不包含 datepicker 吗?
  • View view = (View) LayoutInflater.from(CreateInvoice.this).inflate(R.layout.date, null); DatePicker datepicker = view.findViewById(R.id.datepicker); alt.setView(view);
猜你喜欢
  • 2021-10-12
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多