【发布时间】: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